我应该用什么代替FlowLayout()? [英] What should I use instead of FlowLayout()?

查看:127
本文介绍了我应该用什么代替FlowLayout()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,由于FlowLayout(),我的JFrame并没有达到我想要的方式,但是我不知道该用什么其他方法来解决此问题.它只是使我的JButton充满了整个JFrame.是否可以通过FlowLayout()JFrame组件应用自定义尺寸和位置,还是可以轻松替换的替代方法?

So, my JFrame is not turning out the way I want it to, because of the FlowLayout() but I don't know what else to use to fix this. It just makes my JButton fill the entire JFrame. Is there a way I can get FlowLayout() to apply my custom sizes and location for the JFrame components, or is there an alternative that could easily replace it?

这是我的代码:

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();
        }
        });
    }
}

有什么想法吗?

提前谢谢!

这是问题的图片.

这是我想要的样子(大约):

Here is what I want it to look like (approximately):

推荐答案

本示例将按钮放置在列表下方,并使用滚动窗格在列表中添加边框. GUI中的空白"部分由布局的构造函数中定义的间距(例如,列表和按钮之间的间隔)提供,部分由EmptyBorder提供.

This example puts the button below the list, and adds a border to the list using a scroll pane. 'White space' in the GUI is provided partly by spacing defined in the constructor of the layout (e.g. the space between list and button), and partly by using an EmptyBorder.

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

The control panel (which uses a BorderLayout) is then placed inside the other FlowLayout.

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();
        }
        });
    }
}

这篇关于我应该用什么代替FlowLayout()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆