java - JPanel 组件放置

标签 java swing user-interface jframe jpanel

enter image description here我正在尝试获得一个基本的 GUI 程序。它不需要做太多事情,但底部的按钮必须起作用。我无法将组件(文本、组合框等)放置在正确的位置。使用 GridBag,我可以使用 c.gridx 和 c.gridy 更改位置,但方式非常奇怪。如果我将网格值设置为 0-7,其中 x 为 0,则所有内容都会相互叠加。我尝试通过更改 gridx 值将组合框放在文本旁边,但一切都变得困惑。在我试图移动的组件之后,组件上的对齐已关闭。我该如何解决?我尝试了 BorderLayout.DIRECTION 但没有成功。实际的更改根本不会生效(然后移动到底部)。我该如何解决?谢谢

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaredesign;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;

/**
 *
 * @author 
 */
public class Window extends JFrame {

    //Default global variables
    private JButton submit;
    private JButton cancel;
    private JButton reset;
    private JPanel panel = new JPanel(new GridBagLayout());
    private String searchOutput = "";


    //Default constructor
    public Window() {

        //Title of the window
        super("LIBSYS: Search");

        //Creating the flow layout to which we can work on and adding panel to frame
        setLayout(new FlowLayout());
        add(panel, BorderLayout.SOUTH);

        //Creating the grid to location of the parts
        GridBagConstraints c = new GridBagConstraints();

        //Initializing the buttons
        submit = new JButton("Submit");
        c.insets = new Insets(10, 10, 10, 5);
        c.gridx = 1;
        c.gridy = 20;
        panel.add(submit, c);
        reset = new JButton("Reset");
        c.gridx = 2;
        c.gridy = 20;
        panel.add(reset, c);
        cancel = new JButton("Cancel");
        c.gridx = 3;
        c.gridy = 20;
        panel.add(cancel, c);

        //Handler constructor to do the actionlistening
        Handler handler = new Handler();
        submit.addActionListener(handler);
        reset.addActionListener(handler);
        cancel.addActionListener(handler);

        //Creating the two dropdowns with the words next to them
        JLabel chooseCollection = new JLabel("Choose Collection");
        String[] ccString = {"All", "Mostly", "Some", "Few"};
        JComboBox ccComboBox = new JComboBox(ccString);
        JLabel searchUsing = new JLabel("Search Using");
        String[] suString = {"Title", "Artist", "Arthor", "Type"};
        JComboBox suComboBox = new JComboBox(suString);

        //Adding all the text and dropdown menus to the panel
        c.gridx = 0;
        c.gridy = 24;
        panel.add(chooseCollection, c);
        c.gridx = 0;
        c.gridy = 25;
        panel.add(ccComboBox, c);
        c.gridx = 1;
        c.gridy = 25;
        panel.add(searchUsing, c);
        c.gridx = 0;
        c.gridy = 27;
        panel.add(suComboBox, c);
        c.gridx = 1;
        c.gridy = 27;

        //Setting up the text inputbox
        JLabel keyword = new JLabel("Keyword or phrase");
        JTextField textField = new JTextField(20);

        //Adding lable and text box to the panel
        panel.add(keyword);
        panel.add(textField);
    }

}

最佳答案

我不会尝试强制按钮与其他组件采用相同的布局。它们是独立的,并且应该自由 float 且间隔均匀。

按钮面板应该位于带有flowlayout设置的面板中。然后,应将该面板添加到框架中,borderlayout 位于 SOUTH。

其余组件应放入带有 gridbaglayout 的面板中,并添加到中心的框架中。

然后,gridbaglayout 面板应将其所有组件设置在图片中列出的坐标处。如果组件覆盖两个以上的单元格(即组合),则将 gridWidth 属性设置为 2。

enter image description here

关于java - JPanel 组件放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26796339/

相关文章:

java - 如何在分页式Web表格中找到所需的值?

java - 使用(负)指数分布生成 0 到 1 之间的随机数

java - 如何在 Java 中渲染二维图像

Java:实现一个默认的父类(super class),它将为所有子类设置成员

java - 为什么不推荐使用 "using getString() to get device identifiers"?

java - 从不同的类加载面板

java - 将列从一个 jtable 复制到另一个 jtable

javascript - 以编程方式更改 EditorGrid 的单元格值

python - 如何向 tkinter 窗口添加填充,而不需要 tkinter 将小部件居中?

python - GTK 是否有通用更新通知/发布子系统?