[英]External JPanel classs does not show correctly in Main JFrame class
基本上我正在尝试理解Java中的Threads。所以我想我会创建一个主JFrame类,其中包含两个来自外部类的JPanel,然后在一个中执行某些操作并使用来自第二个面板的消息来控制它。到目前为止我只创建了第一个外部面板,问题开始了! 虽然它似乎是“已加载”但它没有正确显示。(请参阅system.out行)所以这里是主类
package com.maybee.gui;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Maybee extends JFrame implements Runnable
{
public JFrame maynFrame = null;
public JPanel contentPanel = null;
public SimPanel simPanel = null;
public int screenWidth = 0;
public int screenHeight = 0;
public Maybee()
{
}
private void init()
{
System.out.println("In Inint");
maynFrame = new JFrame("Maybee");
maynFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
screenWidth = gd.getDisplayMode().getWidth();
screenHeight = gd.getDisplayMode().getHeight();
maynFrame.setPreferredSize(new Dimension(screenWidth,screenHeight - 100));
maynFrame.setContentPane(getContentPanel());
maynFrame.setVisible(true);
maynFrame.pack();
}
public JPanel getContentPanel()
{
if (contentPanel == null)
{
contentPanel = new JPanel();
contentPanel.setPreferredSize(new Dimension(screenWidth,screenHeight - 100));
contentPanel.setBorder(new LineBorder(Color.BLUE));
contentPanel.setBackground(Color.RED);
contentPanel.setLayout(new BorderLayout());
contentPanel.add(getSimPanel(),BorderLayout.CENTER);
}
return contentPanel;
}
public SimPanel getSimPanel()
{
if(simPanel == null)
{
simPanel = new SimPanel(this);
}
return simPanel;
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
System.out.println("Start");
Maybee maybee = new Maybee();
maybee.run();
}
});
}
public void run()
{
init();
}
}
现在是第一个外部JPanel类
package com.maybee.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class SimPanel extends JPanel
{
public Maybee localMaybee = null;
public JPanel simPanel = null;
private JButton btn;
public SimPanel(Maybee interMaybee)
{
localMaybee = interMaybee;
init();
}
public void init()
{
simPanel = new JPanel();
simPanel.setLayout(new BorderLayout());
simPanel.setPreferredSize(new Dimension(localMaybee.screenWidth/4,localMaybee.screenHeight - 100));
simPanel.setBackground(Color.GREEN);
simPanel.add(getBtn(),BorderLayout.CENTER);
simPanel.setVisible(true);
System.out.println("IN SIM" + localMaybee.screenWidth);
}
public JButton getBtn()
{
if(btn == null)
{
btn = new JButton("ENDE");
btn.setSize(70, 20);
btn.setForeground(Color.YELLOW);
btn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
}
});
}
return btn;
}
}
那我错过了什么?
非常感谢!
当前问题是在SimPanel.init()
创建的JPanel
的第二个实例。 SimPanel
已经是一个JPanel
,没有必要维护public JPanel simPanel
成员。
同样的问题在于扩展JFrame
的Maybee
类,但维护public JFrame maynFrame
成员。
另外,正如上面的评论中已经提到的那样(感谢@Frakcool!):
确保在setVisible()
之前调用pack()
setVisible()
;
不要调用setPreferredSize()
,重写getPreferredSize()
intead;
无需扩展JFrame
;
无需在JPanel
上调用setVisible
;
不要调用btn.setSize()
,它是布局管理器的工作;
不需要setContentPane()
,默认情况下JFrame
将JPanel
作为带有BorderLayout
内容窗格。 在这种情况下调用add()
就足够了。
以下是原始代码的略微修改版本(为简洁起见而简化):
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Maybee2 {
static class SimPanel extends JPanel {
public SimPanel() {
setLayout(new BorderLayout());
JButton btn = new JButton("ENDE");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//TODO
}
});
add(btn, BorderLayout.CENTER);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
}
private static void createAndShowGUI() {
final JFrame frame = new JFrame("Maybee");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SimPanel simPanel = new SimPanel();
frame.add(simPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
编辑 :
该应用程序可能包含许多面板。 诸如JFrame
高级容器不知道所有底层布局复杂性,并且不能强制执行特定大小。 面板本身知道其内部布局及其内容。 因此,面板向布局管理器报告其首选大小,布局管理器最终打包内容。 有关详细信息,请参阅在容器中布置组件 。
setBackground
虽然按钮占据了BorderLayout
的中心,但它占据了面板的所有空间。 更改面板的布局并查看效果。 或者将按钮移动到另一个区域,即 - add(btn, BorderLayout.NORTH);
阅读布局管理器可视指南中的更多内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.