gpt4 book ai didi

java - Netbean java 图形用户界面

转载 作者:行者123 更新时间:2023-12-02 02:32:51 26 4
gpt4 key购买 nike

我是 JFrame 新手,并且在 NetBean 中遇到布局问题,如何解决此布局问题?我尝试调整大小,但元素的位置会变得困惑。我试图将每个细节显示到指定的文本区域。我使用了错误的布局还是我的编码有问题?帮助...><

这是我现在的输出 This is my output

这就是我想要的输出 This is the output i want

这些是我的编码:

public class PokemonJournal extends JFrame
implements ActionListener {

JTextArea FirstName = new JTextArea(1, 10);
JTextArea LastName = new JTextArea(1, 10);
JTextArea Level = new JTextArea(1, 10);
JTextArea Gender = new JTextArea(1, 10);
JButton showPokemon = new JButton("Show");
JButton showRaid = new JButton("Show");
JButton showPokestop = new JButton("Show");
DBHandler db = new DBHandler();
ImageIcon image = new ImageIcon("p1.jpg");
JLabel imageLabel = new JLabel(image);

public static void main(String[] args) {
PokemonJournal jf = new PokemonJournal();
}

public PokemonJournal() {
setLayout(new FlowLayout());
setSize(300, 600);
setTitle(" Pokemon Journal ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);

JPanel top = new JPanel();
add("north", top);
top.add(new Label("Pokemon Journal"));

JPanel side = new JPanel();
add("West", side);
side.setLayout(new GridLayout());
side.add(imageLabel);

JPanel line1 = new JPanel();
add("center", line1);
line1.setLayout(new GridLayout());
line1.add(new Label("First Name :"));
line1.add(FirstName);

JPanel line2 = new JPanel();
add("Center", line2);
FirstName.setEditable(false);
line2.setLayout(new GridLayout());
line2.add(new Label("Last Name :"));
line2.add(LastName);
LastName.setEditable(false);

JPanel line3 = new JPanel();
add("Center", line3);
line3.setLayout(new GridLayout());
line3.add(new Label("Level :"));
line3.add(Level);
Level.setEditable(false);

JPanel line4 = new JPanel();
add("Center", line4);
line4.setLayout(new GridLayout());
line4.add(new Label("Gender : "));
line4.add(Gender);
Gender.setEditable(false);

JPanel line5 = new JPanel();
add("Center", line5);
line5.setLayout(new GridLayout());
line5.add(new Label("Pokemon Caught:"));
line5.add(showPokemon);

JPanel line6 = new JPanel();
add("Center", line6);
line6.setLayout(new GridLayout());
line6.add(new Label("Raid Won:"));
line6.add(showRaid);

JPanel line7 = new JPanel();
add("Center", line7);
line7.setLayout(new GridLayout());
line7.add(new Label("Pokestop visited:"));
line7.add(showPokestop);

}

最佳答案

Am i using the wrong layout or my coding are having problems?

嗯,是的,有一点,但这与你的 JFrame 的大小有更多关系。 ,永远不要打电话setSize()请调用 pack() 。但是,如果将其更改为 pack()FlowLayout 的默认方向是 FlowLayout.HORIZONTAL因此您的所有组件都将位于彼此的右侧。

然后,您有 2 个选择:

  1. 保留JFrame的布局为FlowLayout但做两个JPanel s,一个用于图像,一个用于数据,图像可能具有默认值 FlowLayout也一样,但另一个可能有 GridLayout .

  2. 使用GridBagLayout对于所有组件,让图像 JLabel高度为 7 个单元格,所有其他单元格的高度为 1。

对于以下示例,我将使用 GridBagLayout选项,我建议您尝试制作多面板选项(选项 1)。

但是,在开始之前我必须提及代码中的其他问题:

  • 您没有遵循Java naming conventions :firstWordLowerCaseVariable , firstWordLowerCaseMethods() , FirstWordUpperCaseClasses , ALL_WORDS_UPPER_CASE_CONSTANTS ,这将使您和我们更容易阅读和理解您的代码。

  • 您不会更改 JFrame 的行为因此,无需继承它:请参阅 Extends JFrame vs. creating it inside the program了解更多信息。

  • 与上述观点相关,明智的做法是将 GUI 构建为 JPanel s 而不是 JFrame s,请参阅:The Use of Multiple JFrames: Good or Bad Practice?同样,我认为您将来可能会遇到类似的问题。

  • 您没有将程序放在 Event Dispatch Thread (EDT) 上,请参阅此 related answer看看如何解决这个问题。

  • 您正在调用 setSize(...)方法,正如我之前所说,最好调用 pack()方法,将 GUI 大小的计算留给布局管理器。

  • 您正在调用 setVisible在将所有组件添加到 GUI 之前,这可能会导致您出现空白或黑屏,它应该是程序中的最后一行。

  • 与上述观点相关,我总是喜欢从内到外开始制作 GUI,并以这种方式添加项目,而不是从外到内。

  • 你为什么使用 JTextArea s 而不是 JTextField s 用于用户信息?

现在,使用我给您的第二个选项遵循上述建议的代码是这样的:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class PokemonJournal {
private JFrame frame;
private JPanel pane;
private GridBagConstraints gbc;
private JLabel imageLabel;
private JLabel firstName;
private JLabel lastName;
private JLabel level;
private JLabel gender;
private JLabel pokemonCaught;
private JLabel raidWon;
private JLabel pokestopVisited;
private JTextField nameField;
private JTextField lastNameField;
private JTextField levelField;
private JTextField genderField;
private JButton pokemonCaughtButton;
private JButton raidWonButton;
private JButton pokestopVisitedButton;

public static void main(String[] args) {
SwingUtilities.invokeLater(new PokemonJournal()::createAndShowGui);
}

private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
pane = new JPanel();
pane.setLayout(new GridBagLayout());

gbc = new GridBagConstraints();

imageLabel = new JLabel();
try {
imageLabel.setIcon(new ImageIcon(
new URL("http://pm1.narvii.com/6535/1b362404d09091a335ce53fa68506827418890a3_128.jpg")));
} catch (MalformedURLException e) {
e.printStackTrace();
}

firstName = new JLabel("First Name: ");
lastName = new JLabel("Last Name: ");
level = new JLabel("Level: ");
gender = new JLabel("Gender: ");
pokemonCaught = new JLabel("Pokemon Caught: ");
raidWon = new JLabel("Raid Won: ");
pokestopVisited = new JLabel("Pokestop Visited: ");

nameField = new JTextField(10);
lastNameField = new JTextField(10);
levelField = new JTextField(10);
genderField = new JTextField(10);

pokemonCaughtButton = new JButton("Show");
raidWonButton = new JButton("Show");
pokestopVisitedButton = new JButton("Show");

gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 7;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);

pane.add(imageLabel, gbc);

gbc.gridx = 1;
gbc.gridheight = 1;
pane.add(firstName, gbc);

gbc.gridx = 2;
pane.add(nameField, gbc);

gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(lastName, gbc);

gbc.gridx = 2;
pane.add(lastNameField, gbc);

gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(level, gbc);

gbc.gridx = 2;
pane.add(levelField, gbc);

gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(gender, gbc);

gbc.gridx = 2;
pane.add(genderField, gbc);

gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(pokemonCaught, gbc);

gbc.gridx = 2;
pane.add(pokemonCaughtButton, gbc);

gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(raidWon, gbc);

gbc.gridx = 2;
pane.add(raidWonButton, gbc);

gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(pokestopVisited, gbc);

gbc.gridx = 2;
pane.add(pokestopVisitedButton, gbc);

frame.add(pane);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

它会生成一个与此类似的 GUI:

enter image description here

关于java - Netbean java 图形用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46833193/

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