Java 为什么我的面板没有显示在JFrame中 公共类InputPanel扩展了JPanel{ 公共静态int-shapeType;//1:矩形;2:椭圆形;3:直线 public static boolean isFilled;//形状是否已填充 公共静态颜色;//形状的颜色 公共输入面板(){ JPanel面板=新的JPanel(新的FlowLayout(FlowLayout.LEFT)); 面板.立根背景(颜色.灰色); setPreferredSize(新尺寸(200500)); JButton rect=新JButton(“矩形”); JButton oval=新JButton(“椭圆形”); JButton line=新JButton(“line”); JRadioButton fill=新的JRadioButton(“Filled:”); JButton color1=新JButton(“颜色”); addActionListener(新的rectListener()); addActionListener(新的ovalListener()); addActionListener(newlineListener()); isFilled=fill.isSelected(); color1.addActionListener(新的colorListener()); 面板。添加(rect); 面板。添加(椭圆形); 面板。添加(行); 面板。添加(填充); 面板。添加(颜色1); 此.setVisible(true); } } 公共类PaintPanel扩展了JPanel{ 公共静态int x1、y1、x2、y2; ArrayList shapeList=新的ArrayList(); 公共绘画小组(){ JPanel面板=新的JPanel(新的FlowLayout(FlowLayout.RIGHT)); 面板.立根背景(颜色.白色); setPreferredSize(新尺寸(500500)); this.addMouseListener(新的MouseAdapter(){ @覆盖公共无效鼠标按下(MouseEvent e){ PaintPanel.x1=e.getX(); PaintPanel.y1=e.getY(); } @覆盖公共无效MouseEvent(MouseEvent e){ PaintPanel.x2=e.getX(); PaintPanel.y2=e.getY(); 如果(InputPanel.shapeType==1){ 添加(新矩形(PaintPanel.x1、PaintPanel.y1、PaintPanel.x2、PaintPanel.y2、InputPanel.isFilled)); } if(InputPanel.shapeType==2){ 添加(新椭圆形(PaintPanel.x1、PaintPanel.y1、PaintPanel.x2、PaintPanel.y2、InputPanel.isFilled)); } 如果(InputPanel.shapeType==3){ 添加(新行(PaintPanel.x1、PaintPanel.y1、PaintPanel.x2、PaintPanel.y2)); } 重新油漆(); } }); 此.setVisible(true); } @凌驾 公共组件(图形g){ 超级组件(g); 对于(形状s:形状列表){ s、 抽签(g); } } } 公共级画师{ 公共静态void main(字符串[]args){ JFrame-frame=新的JFrame(“形状抽屉!”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(新的InputPanel()); frame.add(新的PaintPanel()); frame.pack(); frame.setVisible(true); } }
我肯定我已经正确地创建了JFrame,并且我的所有其他类都正常工作,但是这里一定有我遗漏的东西。。。Java 为什么我的面板没有显示在JFrame中 公共类InputPanel扩展了JPanel{ 公共静态int-shapeType;//1:矩形;2:椭圆形;3:直线 public static boolean isFilled;//形状是否已填充 公共静态颜色;//形状的颜色 公共输入面板(){ JPanel面板=新的JPanel(新的FlowLayout(FlowLayout.LEFT)); 面板.立根背景(颜色.灰色); setPreferredSize(新尺寸(200500)); JButton rect=新JButton(“矩形”); JButton oval=新JButton(“椭圆形”); JButton line=新JButton(“line”); JRadioButton fill=新的JRadioButton(“Filled:”); JButton color1=新JButton(“颜色”); addActionListener(新的rectListener()); addActionListener(新的ovalListener()); addActionListener(newlineListener()); isFilled=fill.isSelected(); color1.addActionListener(新的colorListener()); 面板。添加(rect); 面板。添加(椭圆形); 面板。添加(行); 面板。添加(填充); 面板。添加(颜色1); 此.setVisible(true); } } 公共类PaintPanel扩展了JPanel{ 公共静态int x1、y1、x2、y2; ArrayList shapeList=新的ArrayList(); 公共绘画小组(){ JPanel面板=新的JPanel(新的FlowLayout(FlowLayout.RIGHT)); 面板.立根背景(颜色.白色); setPreferredSize(新尺寸(500500)); this.addMouseListener(新的MouseAdapter(){ @覆盖公共无效鼠标按下(MouseEvent e){ PaintPanel.x1=e.getX(); PaintPanel.y1=e.getY(); } @覆盖公共无效MouseEvent(MouseEvent e){ PaintPanel.x2=e.getX(); PaintPanel.y2=e.getY(); 如果(InputPanel.shapeType==1){ 添加(新矩形(PaintPanel.x1、PaintPanel.y1、PaintPanel.x2、PaintPanel.y2、InputPanel.isFilled)); } if(InputPanel.shapeType==2){ 添加(新椭圆形(PaintPanel.x1、PaintPanel.y1、PaintPanel.x2、PaintPanel.y2、InputPanel.isFilled)); } 如果(InputPanel.shapeType==3){ 添加(新行(PaintPanel.x1、PaintPanel.y1、PaintPanel.x2、PaintPanel.y2)); } 重新油漆(); } }); 此.setVisible(true); } @凌驾 公共组件(图形g){ 超级组件(g); 对于(形状s:形状列表){ s、 抽签(g); } } } 公共级画师{ 公共静态void main(字符串[]args){ JFrame-frame=新的JFrame(“形状抽屉!”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(新的InputPanel()); frame.add(新的PaintPanel()); frame.pack(); frame.setVisible(true); } },java,swing,jframe,jpanel,visibility,Java,Swing,Jframe,Jpanel,Visibility,我肯定我已经正确地创建了JFrame,并且我的所有其他类都正常工作,但是这里一定有我遗漏的东西。。。 当我运行main方法时,我得到的只是一个明显是正方形的灰色框(500x500,正如在PaintPanel类中实例化的那样。我做错了什么?除了Andrew提到的,我注意到在InputPanel和PaintPanel中都创建了一个新的JPanel。你肯定是在向这个面板添加新组件,但最后你没有将这个JPanel本身添加到InputPanel或PaintPanel中。因此,make确保在这些面板的构造函
当我运行main方法时,我得到的只是一个明显是正方形的灰色框(500x500,正如在PaintPanel类中实例化的那样。我做错了什么?除了Andrew提到的,我注意到在InputPanel和PaintPanel中都创建了一个新的JPanel。你肯定是在向这个面板添加新组件,但最后你没有将这个JPanel本身添加到InputPanel或PaintPanel中。因此,make确保在这些面板的构造函数中,在末尾有一个
add(panel)
另外,请注意,Swing中的大多数操作都不是线程安全的,因此在创建UI组件/与UI组件交互之前请先阅读相关内容。换句话说,用户界面的任何更新都必须在事件调度线程上进行,如启动应用程序:
public class InputPanel extends JPanel{
public static int shapeType; //1: Rectangle; 2: Oval; 3: Line
public static boolean isFilled; //whether or not the shape is filled
public static Color color; //color of the shape
public InputPanel(){
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel.setBackground(Color.GRAY);
setPreferredSize(new Dimension(200,500));
JButton rect = new JButton("Rectangle");
JButton oval = new JButton("Oval");
JButton line = new JButton("Line");
JRadioButton fill = new JRadioButton("Filled:");
JButton color1 = new JButton("Color..");
rect.addActionListener(new rectListener());
oval.addActionListener(new ovalListener());
line.addActionListener(new lineListener());
isFilled = fill.isSelected();
color1.addActionListener(new colorListener());
panel.add(rect);
panel.add(oval);
panel.add(line);
panel.add(fill);
panel.add(color1);
this.setVisible(true);
}
}
public class PaintPanel extends JPanel{
public static int x1, y1, x2, y2;
ArrayList<Shape> shapeList = new ArrayList<Shape>();
public PaintPanel(){
JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel.setBackground(Color.WHITE);
setPreferredSize(new Dimension(500, 500));
this.addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
PaintPanel.x1 = e.getX();
PaintPanel.y1 = e.getY();
}
@Override public void mouseReleased(MouseEvent e) {
PaintPanel.x2 = e.getX();
PaintPanel.y2 = e.getY();
if(InputPanel.shapeType == 1){
shapeList.add(new Rectangle(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2, InputPanel.isFilled));
}
if(InputPanel.shapeType == 2){
shapeList.add(new Oval(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2, InputPanel.isFilled));
}
if(InputPanel.shapeType == 3){
shapeList.add(new Line(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2));
}
repaint();
}
});
this.setVisible(true);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
for(Shape s : shapeList){
s.draw(g);
}
}
}
public class PaintGUI {
public static void main(String[] args){
JFrame frame = new JFrame("Shape Drawer!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new InputPanel());
frame.add(new PaintPanel());
frame.pack();
frame.setVisible(true);
}
}
默认情况下,JFrame使用BorderLayout
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Shape Drawer!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set the layout, add your panels
frame.pack();
frame.setVisible(true);
}
});
}
等于说,
frame.add(new InputPanel());
frame.add(new PaintPanel());
最终结果是,如果其余代码正常工作,最后添加的面板将是可见的面板。要更快获得更好的帮助,请发布一个。我很难在代码中找到
JFrame
。为什么要执行setVisible(true)
在您的面板上?我添加了JFrame,并将其设置为true messing around,以尝试解决面板未显示的问题frame.add(new InputPanel());frame.add(new PaintPanel());
frame的默认布局将只包含一个没有约束的组件。将其更改为类似GridLayout(1,0)的内容
1.class PaintPanel在类中,您在此处创建了一个未添加到此面板的面板添加以下语句this.add(panel);2.InputPanel:与上面的相同。add(panel);最后您没有为JFrame和JPanel设置LayoutManage,因此需要设置它also@user1787458:很乐意帮忙:)
frame.add(new InputPanel(), BorderLayout.CENTER);
frame.add(new PaintPanel(), BorderLayout.CENTER);