简体   繁体   English

JPanel大小未设置

[英]JPanel size not being set

I'm currently working on an irc bot and today I would like to create a GUI for it. 我正在研究一个irc机器人,今天我想为它创建一个GUI。

I'm trying to create a column layout that has two parts, left and right. 我正在尝试创建一个包含左右两个部分的列布局。 The left side will show console output, the right will contain all the controls (joining channels, commands etc). 左侧将显示控制台输出,右侧将包含所有控件(加入通道,命令等)。

I'm having issues creating the two columns. 我在创建两列时遇到问题。 I have a JPanel that is the whole width and height of the window and has a border of 10 pixels, and then I have two panels within that; 我有一个JPanel,它是窗口的整个宽度和高度,并有一个10像素的边框,然后我有两个面板; left and right. 左和右。

The left and right panels are for some reason taking the whole size of the window, and the right panel is overlapping everything. 左侧和右侧面板由于某种原因占据了窗口的整个大小,右侧面板重叠了所有内容。

Here's an example picture: http://i.imgur.com/lc4vHVH.png 这是一个示例图片: http//i.imgur.com/lc4vHVH.png The white is the right panel, it should only be half the size and have an identical but black panel on the left of it. 白色是右侧面板,它应该只有一半大小,并且左侧有一个相同但黑色的面板。

Here's my current code, sorry if it's messy, new to the whole Swing GUI.. Thanks for any help. 这是我当前的代码,对不起,如果它是凌乱的,整个Swing GUI的新功能..感谢您的帮助。

package tk.TaylerKing.GribbyBot;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.alee.laf.WebLookAndFeel;
import com.alee.laf.menu.WebMenuBar;
import com.alee.laf.menu.WebMenuItem;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.rootpane.WebFrame;

public class GribbyBot extends WebFrame {
    private static final long serialVersionUID = 4641597667372956773L;
    public static HashMap<String, ArrayList<String>> connections = new HashMap<String, ArrayList<String>>();
    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(WebLookAndFeel.class.getCanonicalName());
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                GribbyBot gb = new GribbyBot();
                gb.setVisible(true);
            }
        });
    }
    public GribbyBot(){
        WebPanel panel = new WebPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        panel.setPreferredSize(new Dimension(780, 580));

        WebMenuBar menu = new WebMenuBar();
        WebMenuItem file = new WebMenuItem("Exit");
        file.setMnemonic(KeyEvent.VK_E);
        file.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                System.exit(0);             
            }
        });
        menu.add(file);
        setJMenuBar(menu);

        WebPanel left = new WebPanel();
        left.setPreferredSize(new Dimension(380, 580));
        left.setBackground(Color.BLACK);

        WebPanel right = new WebPanel();
        right.setPreferredSize(new Dimension(380, 580));
        right.setBackground(Color.WHITE);

        add(panel);
        panel.add(left);
        panel.add(right);
        setTitle("GribbyBot");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
    }
}

On a side note, all the variables that are prefixed with "Web" are the same as Swing, but it's a custom GUI. 另外,所有以“Web”为前缀的变量都与Swing相同,但它是一个自定义GUI。

Override JComponent#getPreferredSize() instead of using setPreferredSize() 覆盖JComponent#getPreferredSize()而不是使用setPreferredSize()

Read more Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? 阅读更多我应该避免在Java Swing中使用set(Preferred | Maximum | Minimum)Size方法吗?

If extremely needed in case of Performing Custom Painting then try in this way: 如果在执行自定义绘画时非常需要,请尝试以这种方式:

Sample code: 示例代码:

final JPanel panel = new JPanel(){
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        // your custom painting code here
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(40, 40);
    }
};

Why are using setPreferredSize() method whereas you can achieve this design easily using any proper layout such as BoxLayout , GridLayout , BorderLayout etc. 为什么使用setPreferredSize()方法,而您可以使用任何适当的布局轻松实现此设计,如BoxLayoutGridLayoutBorderLayout等。

Read more about layout How to Use Various Layout Managers 阅读有关布局的更多信息如何使用各种布局管理器

EDIT 编辑

try JPanel panel = new JPanel(new GridLayout(1,2)); 尝试JPanel panel = new JPanel(new GridLayout(1,2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM