如何在java中修改当前的JPanel和JFrame
我正在使用以下代码:如何在java中修改当前的JPanel和JFrame,java,swing,Java,Swing,我正在使用以下代码: class ButtonPanel extends JPanel implements ActionListener { public ButtonPanel() { yellowButton=new JButton("Yellow"); blueButton=new JButton("Blue"); redButton=new JButton("Red"); add(yellowButton);
class ButtonPanel extends JPanel implements ActionListener
{
public ButtonPanel()
{
yellowButton=new JButton("Yellow");
blueButton=new JButton("Blue");
redButton=new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
yellowButton.addActionListener(this);
blueButton.addActionListener(this);
redButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
Object source=evt.getSource();
Color color=getBackground();
if(source==yellowButton) color=Color.yellow;
else if (source==blueButton) color=Color.blue;
else if(source==redButton) color=Color.red;
setBackground(color);
repaint();
}
private JButton yellowButton;
private JButton blueButton;
private JButton redButton;
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(400,400);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(new ButtonPanel());
}
}
public class ButtonTest
{
public static void main(String args[])
{
JFrame frame=new ButtonFrame();
frame.show();
}
}
在
actionperformed()
中,我想修改我的面板并添加更多的组件,有什么方法可以做到这一点吗?hmmm我很难评论任何东西,有很多错误,请从这段代码开始
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ButtonPanel extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton yellowButton;
private JButton blueButton;
private JButton redButton;
public ButtonPanel() {
yellowButton = new JButton("Yellow");
yellowButton.addActionListener(this);
blueButton = new JButton("Blue");
blueButton.addActionListener(this);
redButton = new JButton("Red");
redButton.addActionListener(this);
add(yellowButton);
add(blueButton);
add(redButton);
setPreferredSize(new Dimension(400, 400));
}
@Override
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
Color color = getBackground();
if (source == yellowButton) {
color = Color.yellow;
} else if (source == blueButton) {
color = Color.blue;
} else if (source == redButton) {
color = Color.red;
}
setBackground(color);
revalidate();
repaint();
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("ButtonTest");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.add(new ButtonPanel());
frame.pack();
frame.setVisible(true);
}
});
}
}
对
只需为面板调用add(),然后重新验证()和重新绘制()
但是在actionperformed()中,我想修改我的面板并添加更多的组件。。。有没有办法做到这一点
是的,您可以通过add(…)方法将组件添加到JPanel或“this”,并且您需要调用revalidate(),然后有时在JPanel(this)上重新绘制()(特别是如果您还删除了组件)。但在您这样做之前,如果您还没有这样做,我认为您需要阅读有关使用的教程,以便添加位置良好的组件。@StanislavL phaaaaaaaa,但仅此而已。时间+1事实上这不是第一次:-)正如我提到的,我懒得发表评论。所以我默默地加上+1