gpt4 book ai didi

java - 无法在 Java swing 面板中的文本字段下方获取 bordertitle?

转载 作者:行者123 更新时间:2023-11-29 07:06:14 24 4
gpt4 key购买 nike

我一直在编写和重构 Hangman 游戏的代码。我现在基本上有两个类(class)。一个是启动刽子手(包含主要),另一个是让面板插入各种组件。总的来说,我将有一个框架和一个面板、几个按钮以及一个标签和文本字段。我在下面粘贴了我的两段代码。我想把所有的东西都推到框架上,中间的标签和文本字段,我需要顶部的空白空间来放更多的东西。我尝试了 BorderLayout.SOUTH,但没有帮助。边框标题到处都是,但我希望它只围绕单选按钮。请运行代码看看我的意思。谢谢代码 1:使用 Main 方法

import javax.swing.*;
import javax.swing.text.MaskFormatter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.text.ParseException;

public class HangmanGUI {

DetailsPanel myPanel;

public HangmanGUI() throws ParseException {

myPanel = new DetailsPanel();
JFrame myframe = new JFrame();
myframe.getContentPane().setLayout(new BorderLayout());
myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
myframe.setTitle("Hangman Game");
myframe.setVisible(true);
myframe.setLocationRelativeTo(null);
myframe.pack();
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) throws ParseException {
new HangmanGUI();
}
}

代码2:带面板和组件

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;

public class DetailsPanel extends JPanel {

public DetailsPanel() {

setPreferredSize(new Dimension(400, 600));

setBorder(BorderFactory.createTitledBorder(" ciick here "));

createFormattedPanel();

for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
String buttonText = String.valueOf(alphabet);
JButton letterButton = new JButton(buttonText);
letterButton.addActionListener(clickedbutton());
this.add(letterButton, BorderLayout.CENTER);
}

}

private ActionListener clickedbutton() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
System.out.println("actionCommand is: " + actionCommand);
}
};
}

public void createFormattedPanel() {
MaskFormatter formatter = null;
try {
JLabel label = new JLabel("Guesss");
formatter = new MaskFormatter("? ? ? ? ? ? ?");
formatter.setPlaceholderCharacter('?');
JFormattedTextField input = new JFormattedTextField(formatter);
input.setColumns(20);
this.add(label);
this.add(input);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
}
}

最佳答案

我会使用图像 JPanel 来保存刽子手绘图,并充当占位符并将其放置在 JFrame 的 BorderLayout.CENTER 位置。我还将通过使用布局管理器而不是依赖默认的 FlowLayout 来清理南面的 JPanel。例如,南部 Jpanel 的 BorderLayout,将猜测放在页面起始槽中,将按钮放在中心槽中,然后将按钮放入 GridLayout JPanel 中。例如:

import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.text.ParseException;

public class HangmanGUI {
private DetailsPanel myPanel;
private ImagePanel imagePanel = new ImagePanel();

public HangmanGUI() throws ParseException {
myPanel = new DetailsPanel();
JFrame myframe = new JFrame();
// myframe.getContentPane().setLayout(new BorderLayout());
myframe.getContentPane().add(imagePanel, BorderLayout.CENTER);
myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
myframe.setTitle("Hangman Game");
// myframe.setVisible(true);
// myframe.setLocationRelativeTo(null);
myframe.pack();
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.setLocationRelativeTo(null);
myframe.setVisible(true);
}

public static void main(String[] args) throws ParseException {
new HangmanGUI();
}
}

class ImagePanel extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final String TITLE = "Hangman Image";

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

public ImagePanel() {
setBorder(BorderFactory.createTitledBorder(TITLE));
}
}

class DetailsPanel extends JPanel {
public DetailsPanel() {
setLayout(new BorderLayout());

setBorder(BorderFactory.createTitledBorder(" ciick here "));
add(createFormattedPanel(), BorderLayout.PAGE_START);

JPanel letterPanel = new JPanel(new GridLayout(0, 5));
for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
String buttonText = String.valueOf(alphabet);
JButton letterButton = new JButton(buttonText);
letterButton.addActionListener(clickedbutton());
letterPanel.add(letterButton, BorderLayout.CENTER);
}
add(letterPanel, BorderLayout.CENTER);
}

private ActionListener clickedbutton() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
System.out.println("actionCommand is: " + actionCommand);
}
};
}

public JPanel createFormattedPanel() {
JPanel panel = new JPanel();
MaskFormatter formatter = null;
try {
JLabel label = new JLabel("Guesss");
formatter = new MaskFormatter("? ? ? ? ? ? ?");
formatter.setPlaceholderCharacter('?');
JFormattedTextField input = new JFormattedTextField(formatter);
input.setColumns(20);
// this.add(label);
// this.add(input);
panel.add(label);
panel.add(input);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}

return panel;
}
}

此外,在全部添加和全部打包之前,不要设置组件的位置或将其设置为可见。不要设置组件的大小,而是让首选大小和布局管理器为您设置大小。


编辑
你问:

first of all thank you for helping out. This is the solution I wanted. I have some basic questions though. why did you comment out // myframe.getContentPane().setLayout(new BorderLayout());

默认情况下,JFrame 的 contentPane 已经使用 BorderLayout,因此无需显式将其设置为此布局。

also you created three panels, which is nice. for imagepanel, you specified the dimension. but for detailsPanel, you did not specify the dimension. does it mean?

我认为图像将具有确定的大小,因此我重写了它的 getPreferredSize 以便它足够大以显示图像。所有其他组件,我让它们根据组件的首选大小和布局管理器自行调整大小。

if I have many panels and I specify dimension(size) for one panel, all others will get that default on the frame.

同样,所有其他人将根据其组件的首选尺寸和布局管理器拥有自己的首选尺寸。

关于java - 无法在 Java swing 面板中的文本字段下方获取 bordertitle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19438729/

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