private class showAction implements ActionListener
{
}
里面
{
}
里面
解决方案 »
- <T extends Comparable<? super T>>
- 关于java 转成wsdl文件的问题
- for(File f:fileList)是什么意思?
- Myeclipse 8.5
- 用java怎么实现定时运行某个程序?
- 怎么判断一个字符是不是中文吖
- 如何把Date型的数据转换为C++兼容的int型数据?在线!
- 我在调用FtpClient类的put(filename)函数时,报错???同样的代码为什么有的计算机上可以执行,有的就报异常?高分求救!
- 关于DB的,进来看看把,不会吃亏的!
- 小弟不才,写了个不怎么样的软件,但有幸还得到了几百个download,download过我这软件的都进来数数我的不是,感激感激!
- 这种做法有内存泄露的问题吗?
- 如何让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);
}}