我应该使用什么而不是FlowLayout()?

时间:2013-07-22 20:31:10

标签: java swing jframe flowlayout contentpane

所以,我的JFrame并没有按照我想要的方式进行,因为FlowLayout()但是我不知道还有什么可以解决这个问题。它只是让JButton填满整个JFrame。有没有办法让FlowLayout()JFrame组件应用我的自定义尺寸和位置,还是可以轻松替换它?

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MTGSAMPServerReference extends JFrame implements ActionListener {

    public static Toolkit tk = Toolkit.getDefaultToolkit();
    static int ScrnWidth = ((int) tk.getScreenSize().getWidth());
    static int ScrnHeight = ((int) tk.getScreenSize().getHeight());

    private static final long serialVersionUID = 1L;
    private static JList list1;
    private static JButton next;

    public MTGSAMPServerReference() {
        // set flow layout for the frame
        this.getContentPane().setLayout(new FlowLayout());
        Object[] data1 = { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" };
        list1 = new JList<Object>(data1);
        next = new JButton("Next");
        next.addActionListener(this);
        // add list to frame
        add(list1);
        add(next);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Next")) {
            int index = list1.getSelectedIndex();
            System.out.println("Index Selected: " + index);
            String s = (String) list1.getSelectedValue();
            System.out.println("Value Selected: " + s);
        }
    }
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame f = new MTGSAMPServerReference();
        //Display the window.
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setSize(1200, 800);
        f.setLocationRelativeTo(null);
        list1.setSize(250, 250);
        list1.setLocation(0, 0);
        next.setSize(75, 25);
        next.setLocation(251, 276);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
        createAndShowGUI();
        }
        });
    }
}

有什么想法吗?

提前致谢!

编辑:这是问题的图片。

This is the problem.

编辑:这就是我希望它看起来像(大约):

This is about what it should look like.

2 个答案:

答案 0 :(得分:3)

此示例将按钮放在列表下方,并使用滚动窗格向列表添加边框。 GUI中的“空白空间”部分地由布局的构造函数中定义的间隔(例如,列表和按钮之间的空间)提供,部分地通过使用EmptyBorder来提供。

然后将控制面板(使用BorderLayout)放在另一个FlowLayout内。

GUI

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class MTGSAMPServerReference extends JFrame implements ActionListener {

    public static Toolkit tk = Toolkit.getDefaultToolkit();
    static int ScrnWidth = ((int) tk.getScreenSize().getWidth());
    static int ScrnHeight = ((int) tk.getScreenSize().getHeight());

    private static final long serialVersionUID = 1L;
    private static JList list1;
    private static JButton next;

    public MTGSAMPServerReference() {
        // set flow layout for the frame
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
        Object[] data1 = { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" };

        JPanel controls = new JPanel( new BorderLayout(5,5) );

        list1 = new JList<Object>(data1);
        list1.setVisibleRowCount(5);
        next = new JButton("Next");
        next.addActionListener(this);
        // add list to frame
        controls.add(new JScrollPane(list1));
        controls.add(next, BorderLayout.PAGE_END);
        // adjust numbers as needed.
        controls.setBorder(new EmptyBorder(25,25,0,0));

        add(controls);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Next")) {
            int index = list1.getSelectedIndex();
            System.out.println("Index Selected: " + index);
            String s = (String) list1.getSelectedValue();
            System.out.println("Value Selected: " + s);
        }
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame f = new MTGSAMPServerReference();
        //Display the window.
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setSize(1200, 800);
        f.setLocationRelativeTo(null);
        list1.setSize(250, 250);
        list1.setLocation(0, 0);
        next.setSize(75, 25);
        next.setLocation(251, 276);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
        createAndShowGUI();
        }
        });
    }
}

答案 1 :(得分:-2)

如果要完全控制组件的放置位置并使setBounds有效,则需要删除布局管理器。这可以通过将布局管理器设置为null来完成。但是,这不是一个好主意,而且你最好只使用布局管理器而不使用setBounds,因为你没有被束缚到特定的窗口大小和默认组件大小,以及看起来很好的组件在一个外观上具有特定尺寸,在另一个外观上感觉可能看起来不太好。