gpt4 book ai didi

java - GUI 之间有随机间距

转载 作者:行者123 更新时间:2023-12-01 16:59:43 25 4
gpt4 key购买 nike

我最近开始为我的计算机科学类(class)使用这个 GUI,由于某种原因,我得到的输出完全不一致并且没有任何意义。

以下是我的意思的示例:

在我的代码中,我对使用 GridBagLayout 的按钮有一定的限制。

             GridBagConstraints countrytextRestraints = new GridBagConstraints();
countrytextRestraints.gridx = 1;
countrytextRestraints.gridy = 30;

GridBagConstraints country1restraints = new GridBagConstraints();
country1restraints.gridx = 2;
country1restraints.gridy = 30;

GridBagConstraints country2restraints = new GridBagConstraints();
country2restraints.gridx = 3;
country2restraints.gridy = 30;

GridBagConstraints country3restraints = new GridBagConstraints();
country3restraints.gridx = 4;
country3restraints.gridy = 30;


如您所见,每个按钮的 gridX 值之间的间隔永远不会超过 1 个像素,但由于某种原因,我的输出最终看起来像这样

Output

与其他元素相比,其空间量远远不成比例。这种情况发生在代码中的多个位置。这是怎么回事?

这是我的所有代码以获取更多帮助


package Starting;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class View extends JPanel implements ActionListener {

View(Container pane) {


pane.setLayout(new GridBagLayout());

ImageIcon myImage = new ImageIcon("Elf.jpg");
Image resizedElf = myImage.getImage();
resizedElf = resizedElf.getScaledInstance(80, 60, Image.SCALE_DEFAULT);
myImage = new ImageIcon(resizedElf);
JLabel myImageLabel = new JLabel();
myImageLabel.setIcon(myImage);

GridBagConstraints imageConstraints = new GridBagConstraints();
//X is cols
//Y is row
imageConstraints.gridx = 0;
imageConstraints.gridy = 0;
imageConstraints.gridheight = 1;
imageConstraints.gridwidth = 1;




pane.add(myImageLabel, imageConstraints);




JList theGames = new JList();
JLabel Company = new JLabel("Company");



// The Country buttons
JLabel Country = new JLabel("Country:");
ButtonGroup countryofOrigin = new ButtonGroup();
JRadioButton country1 = new JRadioButton("Japan");
JRadioButton country2 = new JRadioButton("U.S");
JRadioButton country3 = new JRadioButton("Canada");

GridBagConstraints countrytextRestraints = new GridBagConstraints();
countrytextRestraints.gridx = 1;
countrytextRestraints.gridy = 30;

GridBagConstraints country1restraints = new GridBagConstraints();
country1restraints.gridx = 2;
country1restraints.gridy = 30;

GridBagConstraints country2restraints = new GridBagConstraints();
country2restraints.gridx = 3;
country2restraints.gridy = 30;

GridBagConstraints country3restraints = new GridBagConstraints();
country3restraints.gridx = 4;
country3restraints.gridy = 30;



//Model is all of the data
//Methods in controller called through actionlistener
//View is correct
country1.addActionListener(e -> {});
country2.addActionListener(e -> {});
country3.addActionListener(e -> {});

countryofOrigin.add(country1);
countryofOrigin.add(country2);
countryofOrigin.add(country3);


pane.add(country1, country1restraints);
pane.add(country2, country2restraints);
pane.add(country3, country3restraints);
pane.add(Country, countrytextRestraints);

//Genre buttons
JLabel Genre = new JLabel("Genre");
ButtonGroup thegenres = new ButtonGroup();


JRadioButton action = new JRadioButton("Action");
action.addActionListener(event -> { });


JRadioButton RPG = new JRadioButton("RPG");
RPG.addActionListener(event -> { });

JRadioButton Puzzle = new JRadioButton("Puzzle");
Puzzle.addActionListener(event -> { });

thegenres.add(action);
thegenres.add(RPG);
thegenres.add(Puzzle);



GridBagConstraints genreTextconstraints = new GridBagConstraints();
genreTextconstraints.gridx = 1;
genreTextconstraints.gridy = 40;

GridBagConstraints actionRestraints = new GridBagConstraints();
actionRestraints.gridx = 2;
actionRestraints.gridy = 40;

GridBagConstraints RPGconstraints = new GridBagConstraints();
RPGconstraints.gridx = 3;
RPGconstraints.gridy = 40;

GridBagConstraints puzzleConstraints = new GridBagConstraints();
puzzleConstraints.gridx = 4;
puzzleConstraints.gridy = 40;


pane.add(action, actionRestraints);
pane.add(RPG, RPGconstraints);
pane.add(Puzzle, puzzleConstraints);
pane.add(Genre,genreTextconstraints);



JLabel PriceLabel = new JLabel("Price");
JTextField priceText = new JTextField("Enter price");
priceText.addActionListener(event -> { });


JLabel DescriptionLabel = new JLabel("Description");
JTextField Description = new JTextField("Describe the game here!");

JButton cancel = new JButton("cancel");
cancel.addActionListener(e -> { });

JButton save = new JButton("Save");
save.addActionListener(e -> { });

GridBagConstraints priceLabelConstraints = new GridBagConstraints();
priceLabelConstraints.gridx = 1;
priceLabelConstraints.gridy = 20;

GridBagConstraints priceTextConstraints = new GridBagConstraints();
priceTextConstraints.gridx = 2;
priceTextConstraints.gridy = 20;

GridBagConstraints DescriptionLabelConstraints = new GridBagConstraints();
DescriptionLabelConstraints.gridx = 29;
DescriptionLabelConstraints.gridy = 50;

GridBagConstraints DescriptionConstraints = new GridBagConstraints();
DescriptionConstraints.gridx = 30;
DescriptionConstraints.gridy = 50;

GridBagConstraints CancelConstraints = new GridBagConstraints();
CancelConstraints.gridx = 10;
CancelConstraints.gridy = 70;

GridBagConstraints saveConstraints = new GridBagConstraints();
saveConstraints.gridx = 11;
saveConstraints.gridy = 70;

pane.add(PriceLabel,priceLabelConstraints);
pane.add(priceText, priceTextConstraints);
pane.add(DescriptionLabel, DescriptionLabelConstraints);
pane.add(Description, DescriptionConstraints);
pane.add(cancel, CancelConstraints);
pane.add(save, saveConstraints);

JList listofGames = new JList();
listofGames.addListSelectionListener(e -> {});
GridBagConstraints listofGamesconstraints = new GridBagConstraints();
listofGamesconstraints.gridx = 60;
listofGamesconstraints.gridy = 60;
pane.add(listofGames, listofGamesconstraints);


}


@Override
public void actionPerformed(ActionEvent e) {

}

public static void main(String[] args) {

}

}

还有

package Starting;

import javax.swing.*;
import java.awt.*;

public class Starter {


public static void main(String[] args) {
JFrame frame = new JFrame("Example Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(800,600));
mainPanel.setBackground(Color.white);

View theview = new View(frame.getContentPane());
theview.setPreferredSize(new Dimension(800,600));
theview.setLayout(new GridBagLayout());
mainPanel.add(theview);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setVisible(true);




}

}

这就是我想要的样子,供引用

enter image description here

最佳答案

正如所 promise 的。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.WindowConstants;

public class Starters implements Runnable {

@Override // java.lang.Runnable
public void run() {
createAndShowGui();
}

private void createAndShowGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(createForm(), BorderLayout.CENTER);
frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

private JPanel createButtonsPanel() {
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
JButton cancelButton = new JButton("Cancel");
buttonsPanel.add(cancelButton);
JButton saveButton = new JButton("Save");
buttonsPanel.add(saveButton);
return buttonsPanel;
}

private JPanel createCountriesPanel() {
JPanel countriesPanel = new JPanel();
ButtonGroup bg = new ButtonGroup();
JRadioButton northAmerica = new JRadioButton("North America");
JRadioButton japan = new JRadioButton("Japan");
JRadioButton canada = new JRadioButton("Canada");
bg.add(northAmerica);
bg.add(japan);
bg.add(canada);
countriesPanel.add(northAmerica);
countriesPanel.add(japan);
countriesPanel.add(canada);
return countriesPanel;
}

private JPanel createForm() {
JPanel form = new JPanel(new GridBagLayout());
form.setBorder(BorderFactory.createEmptyBorder(5, 0, 10, 15));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets.bottom = 5;
gbc.insets.left = 5;
gbc.insets.right = 5;
gbc.insets.top = 5;
ImageIcon imgIco = new ImageIcon("path-to-your-image-file");
JLabel img = new JLabel(imgIco);
form.add(img, gbc);

gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridx = 1;
gbc.gridy = 1;
JLabel nameLabel = new JLabel("Name");
form.add(nameLabel, gbc);
gbc.gridx = 2;
form.add(createNameSpinner(), gbc);

gbc.gridx = 1;
gbc.gridy = 2;
JLabel companyLabel = new JLabel("Company");
form.add(companyLabel, gbc);
gbc.gridx = 2;
JTextField companyTextField = new JTextField(7);
form.add(companyTextField, gbc);

gbc.gridx = 1;
gbc.gridy = 3;
JLabel countryLabel = new JLabel("Country");
form.add(countryLabel, gbc);
gbc.gridx = 2;
form.add(createCountriesPanel(), gbc);

gbc.gridx = 1;
gbc.gridy = 4;
JLabel typeLabel = new JLabel("Type");
form.add(typeLabel, gbc);
gbc.gridx = 2;
form.add(createTypesPanel(), gbc);

gbc.gridx = 1;
gbc.gridy = 5;
JLabel lengthLabel = new JLabel("Game Length");
form.add(lengthLabel, gbc);
gbc.gridx = 2;
form.add(createSlider(), gbc);

gbc.gridx = 1;
gbc.gridy = 6;
JLabel priceLabel = new JLabel("Price");
form.add(priceLabel, gbc);
gbc.gridx = 2;
JTextField priceTextField = new JTextField(7);
form.add(priceTextField, gbc);

gbc.gridx = 1;
gbc.gridy = 7;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
JLabel descriptionLabel = new JLabel("Description");
form.add(descriptionLabel, gbc);
gbc.gridx = 2;
gbc.anchor = GridBagConstraints.LINE_START;
JTextArea description = new JTextArea(5, 40);
description.setLineWrap(true);
description.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(description);
form.add(scrollPane, gbc);
return form;
}

private JSpinner createNameSpinner() {
SpinnerModel model = new SpinnerListModel(new String[]{"Reuben",
"Simeon",
"Levi",
"Judah",
"Dan",
"Naphtali",
"Gad",
"Asher",
"Issachar",
"Zebulun",
"Joseph",
"Benjamin"});
JSpinner nameSpinner = new JSpinner(model);
return nameSpinner;
}

private JSlider createSlider() {
JSlider slider = new JSlider(30, 100, 50);
Hashtable<Integer, JComponent> labels = new Hashtable<>(2);
labels.put(Integer.valueOf(30), new JLabel("30"));
labels.put(Integer.valueOf(100), new JLabel("100"));
slider.setLabelTable(labels);
slider.setPaintLabels(true);
return slider;
}

private JPanel createTypesPanel() {
JPanel typesPanel = new JPanel();
ButtonGroup bg = new ButtonGroup();
JRadioButton action = new JRadioButton("Action");
JRadioButton rpg = new JRadioButton("RPG");
JRadioButton puzzle = new JRadioButton("Puzzle");
bg.add(action);
bg.add(rpg);
bg.add(puzzle);
typesPanel.add(action);
typesPanel.add(rpg);
typesPanel.add(puzzle);
return typesPanel;
}

public static void main(String[] args) {
EventQueue.invokeLater(new Starters());
}
}

关于java - GUI 之间有随机间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61528308/

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