不确定如何正确使用摆动布局

时间:2014-03-10 12:51:02

标签: java swing button layout

抱歉模糊的标题,想不出更好的东西。我正在尝试创建一个jswing应用程序,它有4个按钮和3个图像框。左边是四个按钮。第一个“SalesTop”,第二个“SalesBottom”,第三个“Initial”和第四个“Next”。右边有三个相同大小的盒子,我可以将图像插入其中,同时能够在其旁边设置文字以区分每张图片。我试过看,但我似乎无法让布局按我的意愿工作。 这就是我到目前为止所拥有的。

private JButton salesTop, salesBottom, preliminary, next;
private JLabel current;
private JPanel salesTopEx, salesBottomEx, preliminaryEx, buttonPanel;
private JFrame mainFrame = new JFrame();

 public  int CreateJPANEL() {
    buttonPanel = new JPanel();
    buttonPanel.setPreferredSize(new Dimension(150, 1000));

    salesTop = new JButton("salesTop");
    salesBottom = new JButton("salesBottom");
    preliminary = new JButton("Preliminary");

    salesTop.addActionListener(new ButtonListener("salesTop"));
    salesBottom.addActionListener(new ButtonListener("salesBottom"));
    preliminary.addActionListener(new ButtonListener("Preliminary"));

    buttonPanel.add(salesTop);
    buttonPanel.add(salesBottom);
    buttonPanel.add(preliminary);

    mainFrame.setLayout(new BorderLayout());
    mainFrame.add(buttonPanel);
    mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    mainFrame.setVisible(true);
    mainFrame.getContentPane();

    return 0;
}

1 个答案:

答案 0 :(得分:2)

  1. JPanel使用BoxLayout JButtons BoxLayout。将ImageIcon设置为垂直。

  2. 对于图像,如果图像大小相同,您只需将JLabel添加到ImageIcon沿添加文本即可。您可以使用label.setHorizontalTextPositionlabel.setVertialTextPosition将文字与ImageIcons的任意位置对齐。如果图像大小不同,您可以搜索SO,有关如何缩放JPanel的一些好答案。您还可以BoxLayout JLabel使用GridLayoutJPanel。或者更好JPanel

  3. 将这两个BorderLayoutBorderLayout.CENTER包裹在另一个CENTER中。您可以在WEST放置其中任何一个,具体取决于哪个EAST将另一个放入JPanelsJPanel

  4. 关键是利用嵌套import java.awt.*; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; import javax.swing.border.*; public class NestJPanels { ImageIcon icon; public NestJPanels() { try { icon = new ImageIcon(new URL("http://sstatic.net/programmers/img/apple-touch-icon.png")); } catch (MalformedURLException ex) { Logger.getLogger(NestJPanels.class.getName()).log(Level.SEVERE, null, ex); } Box box = Box.createVerticalBox(); for (int i = 0; i < 4; i++) { box.add(Box.createVerticalStrut(10)); box.add(new JButton("Button")); } JPanel imagePanel = new JPanel(new GridLayout(0, 1)); for (int i = 0; i < 3; i++) { JLabel label = new JLabel(icon); label.setBorder(new MatteBorder(0, 0, 2, 0, Color.GRAY)); label.setText("StackOverflow"); imagePanel.add(label); } JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(new EmptyBorder(10, 10, 10, 10)); panel.add(box, BorderLayout.WEST); panel.add(imagePanel); JFrame frame = new JFrame(); frame.add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { Logger.getLogger(NestJPanels.class.getName()).log(Level.SEVERE, null, ex); } new NestJPanels(); } }); } } 与不同的布局管理器。您可以根据需要嵌套尽可能多的{{1}}。

    Laying out Components Within a Container

    了解详情

    这是一个例子

    enter image description here

    {{1}}