Java I';我试图在一个JFrame中放置多个组件。
按按钮1将一个方框放入窗格2中,按按钮2将另一个方框放入,但第一个方框消失Java I';我试图在一个JFrame中放置多个组件。,java,swing,Java,Swing,按按钮1将一个方框放入窗格2中,按按钮2将另一个方框放入,但第一个方框消失 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GuiDemo extends JPanel implements ActionListener { JFrame pane1 = new JFrame("pane1"); JFrame pane2 = new JFrame("pane2");
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuiDemo extends JPanel implements ActionListener {
JFrame pane1 = new JFrame("pane1");
JFrame pane2 = new JFrame("pane2");
public GuiDemo(){
pane1.setSize(400,400);
pane1.setLocation(100, 100);
pane2.setSize(400,400);
pane2.setLocation(800, 100);
pane1.setVisible(true);
pane2.setVisible(true);
pane1.setLayout(new FlowLayout());
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
pane1.add(b1);
pane1.add(b2);
b1.setVisible(true);
b1.addActionListener(this);
b2.setVisible(true);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
switch (e.getActionCommand()){
case "Button 1":{
placeCircle pc = new placeCircle(0);
pane2.add(pc);
pane2.setVisible(true);
break;}
case "Button 2":{
placeCircle pc = new placeCircle(1);
pane2.add(pc);
pane2.setVisible(true);
break;}
}
}
public static void main(String[] args) {
new GuiDemo();
}
}
a作为框1和框2之间的偏移量传递
class placeCircle extends JPanel{
int a;
public placeCircle(int a){
this.a = a;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawRect(20+a*100, 20, 20, 20);
}
}
但我的主要问题是,我应该使用painComponent吗
按按钮1将一个方框放入窗格2中,按按钮2将另一个方框放入,但第一个方框消失
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuiDemo extends JPanel implements ActionListener {
JFrame pane1 = new JFrame("pane1");
JFrame pane2 = new JFrame("pane2");
public GuiDemo(){
pane1.setSize(400,400);
pane1.setLocation(100, 100);
pane2.setSize(400,400);
pane2.setLocation(800, 100);
pane1.setVisible(true);
pane2.setVisible(true);
pane1.setLayout(new FlowLayout());
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
pane1.add(b1);
pane1.add(b2);
b1.setVisible(true);
b1.addActionListener(this);
b2.setVisible(true);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
switch (e.getActionCommand()){
case "Button 1":{
placeCircle pc = new placeCircle(0);
pane2.add(pc);
pane2.setVisible(true);
break;}
case "Button 2":{
placeCircle pc = new placeCircle(1);
pane2.add(pc);
pane2.setVisible(true);
break;}
}
}
public static void main(String[] args) {
new GuiDemo();
}
}
JFrame的默认布局管理器是BorderLayout。您正在将组件添加到BorderLayout的中心
,但一次只能显示单个组件,因此您只能看到最后一个组件
我应该使用painComponent吗
是的,但是所有的绘制都需要在该组件的paintComponent()方法中的单个组件中完成
所以基本上你需要保留一个要绘制的对象列表。然后,paintComponent()方法遍历列表并绘制每个对象
查看中的绘制组件示例。以这种方法为例
按按钮1将一个方框放入窗格2中,按按钮2将另一个方框放入,但第一个方框消失
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuiDemo extends JPanel implements ActionListener {
JFrame pane1 = new JFrame("pane1");
JFrame pane2 = new JFrame("pane2");
public GuiDemo(){
pane1.setSize(400,400);
pane1.setLocation(100, 100);
pane2.setSize(400,400);
pane2.setLocation(800, 100);
pane1.setVisible(true);
pane2.setVisible(true);
pane1.setLayout(new FlowLayout());
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
pane1.add(b1);
pane1.add(b2);
b1.setVisible(true);
b1.addActionListener(this);
b2.setVisible(true);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
switch (e.getActionCommand()){
case "Button 1":{
placeCircle pc = new placeCircle(0);
pane2.add(pc);
pane2.setVisible(true);
break;}
case "Button 2":{
placeCircle pc = new placeCircle(1);
pane2.add(pc);
pane2.setVisible(true);
break;}
}
}
public static void main(String[] args) {
new GuiDemo();
}
}
JFrame的默认布局管理器是BorderLayout。您正在将组件添加到BorderLayout的中心
,但一次只能显示单个组件,因此您只能看到最后一个组件
我应该使用painComponent吗
是的,但是所有的绘制都需要在该组件的paintComponent()方法中的单个组件中完成
所以基本上你需要保留一个要绘制的对象列表。然后,paintComponent()方法遍历列表并绘制每个对象
查看中的绘制组件示例。对于这种方法的例子,请考虑使用CAMEL CASE,用字母首字母大写用于您的类名,因为这是爪哇的官方!基本的答案是,不要这样做,你在和布局管理器对抗。相反,你应该有一个单一的组件/面板,它维护一个需要画的对象的列表,然后在它的PruttCe组件方法中,它重复这个列表PoT Posits。请考虑使用CAMEL CASE,用第一个字母大写用于您的类名,因为这是爪哇的官方!基本的答案是,不要这样做,你在和布局管理器对抗。相反,您应该有一个组件/面板,它维护需要绘制的对象列表,然后在其paintComponent方法中,它迭代该列表并绘制它们