gpt4 book ai didi

java - JPanel 组件放置

转载 作者:行者123 更新时间:2023-12-01 06:51:19 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com