如何创建和编辑仅在屏幕右半部分的面板?

时间:2014-04-08 00:18:39

标签: java swing jpanel jbutton

这个类生成一个窗口或框架(我不太清楚正确的标题是什么),左侧有一个后退按钮和一个发货选择按钮。我想添加一个占据框架右半部分的面板。我正在尝试添加一些特定于" Ship Selection"所以我假设一个小组是正确的方法,但不完全确定。提前谢谢。

private class OptionsPanel extends JPanel{
        private Galaga parent;

        public OptionsPanel(Galaga p) {
            super();
            parent = p;

            //layout components however you wish
            setLayout(null);
            JButton backButton = new JButton("<< Back");
            backButton.setBounds(5, 20, 100, 20);
            backButton.setFont(new Font("Arial", Font.PLAIN, 15));
            backButton.setForeground(Color.white);
            backButton.setBackground(Color.black);
            backButton.setOpaque(true);
            backButton.setBorderPainted(false);

            backButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    parent.getLayeredPane().remove(parent.getOptionsPanel());
                    parent.getLayeredPane().add(parent.getButtonPanel(), new Integer(10));
                    parent.invalidate();
                }
            });
            add(backButton);

            JButton shipSelectButton = new JButton("Ship Selection");
            shipSelectButton.setBounds(10, 60, 200, 40);
            shipSelectButton.setFont(new Font("Arial", Font.PLAIN, 20));
            shipSelectButton.setForeground(Color.white);
            shipSelectButton.setBackground(Color.black);
            shipSelectButton.setOpaque(true);
            shipSelectButton.setBorderPainted(false);
            shipSelectButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                }
            });
            add(shipSelectButton);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

最基本的选择是使用GridLayout

GridLayout

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
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.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LayoutExamples {

    public static void main(String[] args) {
        new LayoutExamples();
    }

    public LayoutExamples() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridLayout(0, 2));
                frame.add(new OptionsPanel());
                frame.add(new OtherPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class OtherPane extends JPanel {

        public OtherPane() {
            setBackground(Color.RED);
        }

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

    }

    public class OptionsPanel extends JPanel {

        public OptionsPanel() {
            super();

            //layout components however you wish
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(8, 8, 8, 8);
            gbc.anchor = GridBagConstraints.NORTHWEST;

            JButton backButton = new JButton("<< Back");
            backButton.setFont(new Font("Arial", Font.PLAIN, 15));
            backButton.setForeground(Color.white);
            backButton.setBackground(Color.black);
            backButton.setOpaque(true);
            backButton.setBorderPainted(false);

            backButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                }
            });
            add(backButton, gbc);

            JButton shipSelectButton = new JButton("Ship Selection");
            shipSelectButton.setFont(new Font("Arial", Font.PLAIN, 20));
            shipSelectButton.setForeground(Color.white);
            shipSelectButton.setBackground(Color.black);
            shipSelectButton.setOpaque(true);
            shipSelectButton.setBorderPainted(false);
            shipSelectButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                }
            });
            gbc.insets = new Insets(12, 8, 12, 12);
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.anchor = GridBagConstraints.NORTHWEST;
            gbc.weighty = 1;
            gbc.weightx = 1;
            add(shipSelectButton, gbc);
        }
    }

}

像素完美布局是现代UI设计中的一种幻觉。您无法控制字体甚至单行的呈现方式。渲染管道和DPI设置(例如)的差异将改变平台之间组件的度量/大小要求。

Swing旨在使用布局管理器,适当使用它们。