gpt4 book ai didi

java - 我应该用什么代替 FlowLayout()?

转载 作者:行者123 更新时间:2023-11-30 11:30:27 24 4
gpt4 key购买 nike

所以,由于 FlowLayout(),我的 JFrame 没有按照我想要的方式运行,但我不知道还能用什么来修复这。它只会让我的 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.

最佳答案

此示例将按钮放在列表下方,并使用滚动 Pane 向列表添加边框。 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();
}
});
}
}

关于java - 我应该用什么代替 FlowLayout()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17796827/

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