初学JAVA写代码,请教一个很简单的问题!? private class showAction implements ActionListener{}里面 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private class showAction implements ActionListener{}里面怎么写啊?是用paintComponent吗?哪位行行好,给个代码啊?我只知道下面的方法........ public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2=(Graphics2D)g; double leftx=100; double topy=100; double width=200; double height=150; Rectangle2D rect=new Rectangle2D.Double(leftx,topy,width,height); g2.draw(rect); } 在MainPanel类中重载paintComponent(Graphics g)方法,在其内部写画距形的代码。监听器内部:{ pack();//在这里调用窗体的重画代码} to superberd(小熊维尼): 我在MainPanel类中重载paintComponent(Graphics g)方法,那么不用我点击按钮,frame上直接已经画出矩形了........我还是不明白阿 在MainPanel中声明两个静态变量初始化为0 ,点击batton 后再设为矩形的高和宽的尺寸 根据superberd(小熊维尼)给的思路兄弟现在是这样写的:public MainPanel() { setLayout(new BorderLayout()); JButton showButton =new JButton("Show"); add(showButton,BorderLayout.SOUTH); showAction creatTable =new showAction(); showButton.addActionListener(creatTable); } double leftx = 0; double topy = 0; double width = 0; double height = 0; public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2=(Graphics2D)g; Rectangle2D rect=new Rectangle2D.Double(leftx,topy,width,height); g2.draw(rect); } private class showAction implements ActionListener { public void actionPerformed(ActionEvent event) { leftx = 10; topy = 10; width = 30; height =100; repaint(); } }}总觉得写的有些别扭,哪位能给改改?立刻结贴! import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.awt.geom.*;public class CreatLay { public static void main(String[] args) { MainFrame1 frame = new MainFrame1(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); }}class MainFrame1 extends JFrame{ public MainFrame1() { setTitle("bbb"); setSize(500,500); MainPanel panel=new MainPanel(); Container contentPane=getContentPane(); contentPane.add(panel); }}class MainPanel extends JPanel{ public static int h=0,g=0; public MainPanel() { setLayout(new BorderLayout()); JButton showButton =new JButton("Show"); add(showButton,BorderLayout.SOUTH); showAction creatTable =new showAction(); showButton.addActionListener(creatTable); } private class showAction implements ActionListener { h=150,g=200; pack(); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2=(Graphics2D)g; double leftx=100; double topy=100; //double width=200; //double height=150; Rectangle2D rect=new Rectangle2D.Double(leftx,topy,g,h); g2.draw(rect); }} 如何输出文档的时候可以跟在原文档内容的后面 (讨论帖)继承的是什么 java 集成IE浏览器 面试题目~ [闭月羞花猫]这两天怎么这么忙? 请问Java中如何将一个二进制数转换为十进制数啊? 我没分了,麻烦前辈做一下做下作业题,谢谢了 JAVA程序间隔自动运行的问题 哪位有 "数据结构算法与应用" java版的! 为什么不能执行?一个很小的JAVA测试程序 这种做法有内存泄露的问题吗? 如何让Swing里的菜单项暗掉?
{
}
里面怎么写啊?
是用paintComponent吗?
哪位行行好,给个代码啊?我只知道下面的方法........
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
double leftx=100;
double topy=100;
double width=200;
double height=150;
Rectangle2D rect=new Rectangle2D.Double(leftx,topy,width,height);
g2.draw(rect);
}
监听器内部:
{
pack();//在这里调用窗体的重画代码
}
我在MainPanel类中重载paintComponent(Graphics g)方法,那么不用我点击按钮,frame上直接已经画出矩形了........我还是不明白阿
兄弟现在是这样写的:
public MainPanel()
{
setLayout(new BorderLayout());
JButton showButton =new JButton("Show");
add(showButton,BorderLayout.SOUTH);
showAction creatTable =new showAction();
showButton.addActionListener(creatTable);
}
double leftx = 0;
double topy = 0;
double width = 0;
double height = 0;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
Rectangle2D rect=new Rectangle2D.Double(leftx,topy,width,height);
g2.draw(rect);
}
private class showAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
leftx = 10;
topy = 10;
width = 30;
height =100;
repaint();
}
}
}
总觉得写的有些别扭,哪位能给改改?立刻结贴!
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class CreatLay {
public static void main(String[] args)
{
MainFrame1 frame = new MainFrame1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class MainFrame1 extends JFrame
{
public MainFrame1()
{
setTitle("bbb");
setSize(500,500);
MainPanel panel=new MainPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
}
class MainPanel extends JPanel
{
public static int h=0,g=0;
public MainPanel()
{
setLayout(new BorderLayout());
JButton showButton =new JButton("Show");
add(showButton,BorderLayout.SOUTH);
showAction creatTable =new showAction();
showButton.addActionListener(creatTable);
}
private class showAction implements ActionListener
{
h=150,g=200;
pack();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
double leftx=100;
double topy=100;
//double width=200;
//double height=150;
Rectangle2D rect=new Rectangle2D.Double(leftx,topy,g,h);
g2.draw(rect);
}}