当我更改JLabel的文本时,我对元素的整个GUI定位进行了更改。我是GUI的新手,因此我的知识仅限于它正在做的事情。
package GUI;
import javax.swing.*;
import com.tri_voltage.md.util.Tools;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton b1 = new JButton("1"),b2=new JButton("2"),b3=new JButton("3"),b4=new JButton("4"),b5=new JButton("5"),b6=new JButton("6");
JLabel l1= new JLabel("Label 1"),l2= new JLabel("Label 2"),l3=new JLabel("Label 3"),l4=new JLabel("Label 4"),l5=new JLabel("Label 5"),l6=new JLabel("Label 6");
JPanel mainPane = new JPanel(), adminPane = new JPanel(), messagePane=new JPanel();
JLayeredPane layeredPane = new JLayeredPane();
private final int HEIGHT_0 = 0,HEIGHT_1=30,HEIGHT_2=60;
private final int JBUTTON_WIDTH = 100,JBUTTON_HEIGHT = 25,JBUTTON_RIGHT = 305,JBUTTON_LEFT = 0;
private final int JLABEL_WIDTH =50,JLABEL_HEIGHT =25,JLABEL_LEFT=0,JLABEL_RIGHT=150;
private final int JPANEL_WIDTH = 1000,JPANEL_HEIGHT=1000;
public GUI() {
load();
}
public void load() {
setTitle("Employee Console");
setSize(JPANEL_WIDTH,JPANEL_HEIGHT);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPane = Tools.createPanel(0, 0, JPANEL_WIDTH, 700, new GridBagLayout());
adminPane = Tools.createPanel(0, 700, JPANEL_WIDTH, 300, "Admin Console", new GridBagLayout());
messagePane = Tools.createPanel(105, 0, 200, 90, new GridBagLayout());
loadButtons();
loadLabels();
layeredPane.setPreferredSize(new Dimension(1000,1000));
layeredPane.setSize(1000, 1000);
add(layeredPane);
//refresh();
}
public void loadButtons() {
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weightx = 0.12;
constraints.gridx = 2;
constraints.ipady = 0;
constraints.ipadx = 0;
b1 = Tools.createButton(JBUTTON_LEFT, 0, JBUTTON_WIDTH, JBUTTON_HEIGHT, "Button 1", new ButtonListener(this));
b2 = Tools.createButton(JBUTTON_LEFT, 30, JBUTTON_WIDTH, JBUTTON_HEIGHT, "Button 2", new ButtonListener(this));
b3 = Tools.createButton(JBUTTON_LEFT, 60, JBUTTON_WIDTH, JBUTTON_HEIGHT, "Button 3", new ButtonListener(this));
b4 = Tools.createButton(JBUTTON_RIGHT, 0, JBUTTON_WIDTH, JBUTTON_HEIGHT, "Button 4", new ButtonListener(this));
b5 = Tools.createButton(JBUTTON_RIGHT, 30, JBUTTON_WIDTH, JBUTTON_HEIGHT, "Button 5", new ButtonListener(this));
b6 = Tools.createButton(JBUTTON_RIGHT, 60, JBUTTON_WIDTH, JBUTTON_HEIGHT, "Button 6", new ButtonListener(this));
mainPane.add(b1);
mainPane.add(b2);
mainPane.add(b3);
mainPane.add(b4);
mainPane.add(b5);
mainPane.add(b6);
layeredPane.add(mainPane,1);
}
public void loadLabels() {
l1 = Tools.createLabel(JLABEL_LEFT,HEIGHT_0, JLABEL_WIDTH, JLABEL_HEIGHT, l1.getText());
l2 = Tools.createLabel(JLABEL_LEFT,HEIGHT_1, JLABEL_WIDTH, JLABEL_HEIGHT, l2.getText());
l3 = Tools.createLabel(JLABEL_LEFT,HEIGHT_2, JLABEL_WIDTH, JLABEL_HEIGHT, l3.getText());
l4 = Tools.createLabel(JLABEL_RIGHT,HEIGHT_0, JLABEL_WIDTH, JLABEL_HEIGHT, l4.getText());
l5 = Tools.createLabel(JLABEL_RIGHT,HEIGHT_1, JLABEL_WIDTH, JLABEL_HEIGHT, l5.getText());
l6 = Tools.createLabel(JLABEL_RIGHT,HEIGHT_2, JLABEL_WIDTH, JLABEL_HEIGHT, l6.getText());
messagePane.add(l1);
messagePane.add(l2);
messagePane.add(l3);
messagePane.add(l4);
messagePane.add(l5);
messagePane.add(l6);
layeredPane.add(messagePane,0);
}
public void refresh() {
repaint();
}
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
}
}
}
class ButtonListener implements ActionListener {
private final GUI gui;
private String screen = "main";
ButtonListener(GUI gui) {
this.gui = gui;
}
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "Button 1":
JOptionPane.showMessageDialog(new JFrame(),"Button 1 has been pressed!");
break;
case "Button 2":
gui.l1.setText(null);
break;
}
}
}
工具:
package com.tri_voltage.md.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.MenuListener;
import com.tri_voltage.md.util.MediaType;
public class Tools {
private final static Font defaultFont = new Font("Verdana", Font.PLAIN, 15);
private final static ImageIcon defaultImageIcon = null;//TODO: Find a suitable image icon
public static ImageIcon loadMediaTypeImageIcon(String type) {
return loadMediaTypeImageIcon(MediaType.valueOf(type));
}
public static ImageIcon loadMediaTypeImageIcon(MediaType type) {
return loadImageIcon(type.getImagePath());
}
public static ImageIcon loadImageIcon(String path) {
if (path.equalsIgnoreCase("NONE"))
return defaultImageIcon;
return new ImageIcon(Toolkit.getDefaultToolkit().createImage(ClassLoader.getSystemResource(path)));
}
public static String readLine(InputStream stream, long lineNumber) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
for (long l = 0; l < lineNumber; l ++)
reader.readLine();
String line = reader.readLine();
return line;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* Gets the last line number within the file (fileLocation) that is being called.
* @param fileLocation
* @return The last line number of the file. <b>(NOTE: Keep in mind that the first line number of a file is 0!)</b>
* @throws IOException
*/
public static int getLastLineNumber(String location) {
try {
@SuppressWarnings("resource")
LineNumberReader lnr = new LineNumberReader(new FileReader(new File(location)));
lnr.skip(9223372036854775807L);
return lnr.getLineNumber();
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
public static JPanel createPanel(int x, int y, int width, int height, String title, LayoutManager layout, JComponent... components) {
JPanel panel = createPanel(x, y, width, height, layout);
if (title != null || title != "")
panel.setBorder(BorderFactory.createTitledBorder(title));
if (components != null)
for (JComponent component : components)
if (component != null)
panel.add(component);
return panel;
}
public static JPanel createPanel(int x, int y, int width, int height, LayoutManager layout, JComponent... components) {
JPanel panel = createPanel(x, y, width, height, layout);
if (components != null)
for (JComponent component : components)
if (component != null)
panel.add(component);
return panel;
}
public static JPanel createPanel(int x, int y, int width, int height, LayoutManager layout) {
JPanel panel = new JPanel();
panel.setLayout(layout);
panel.setBounds(x, y, width, height);
return panel;
}
public static JButton createButton(int x, int y, int width, int height, String title, ActionListener listener) {
JButton button = new JButton();
if (title == null || title == "")
button.setText("Button");
else
button.setText(title);
button.setBounds(x, y, width, height);
if (listener != null)
button.addActionListener(listener);
return button;
}
public static JTextField createTextField(int x, int y, int width, int height, String actionCommand, KeyListener keyListener, ActionListener actionListener) {
JTextField textField = new JTextField();
textField.setBounds(x, y, width, height);
if (keyListener != null)
textField.addKeyListener(keyListener);
if (actionListener != null)
textField.addActionListener(actionListener);
if (actionCommand != null)
textField.setName(actionCommand);
return textField;
}
public static JTextField createTextField(int x, int y, int width, int height, String actionCommand, KeyListener listener) {
JTextField textField = new JTextField();
textField.setBounds(x, y, width, height);
if (listener != null)
textField.addKeyListener(listener);
if (actionCommand != null)
textField.setName(actionCommand);
return textField;
}
public static JCheckBox createCheckBox(int x, int y, int width, int height, String name, ActionListener listener) {
JCheckBox checkBox = new JCheckBox(name);
checkBox.setBounds(x, y, width, height);
if (listener != null)
checkBox.addActionListener(listener);
return checkBox;
}
public static JComboBox<String> createComboBox(int x, int y, int width, int height, String actionCommand, String[] options, ActionListener listener) {
JComboBox<String> comboBox = new JComboBox<String>(options);
comboBox.setBounds(x, y, width, height);
if (actionCommand != null)
comboBox.setActionCommand(actionCommand);
return comboBox;
}
public static JLabel createLabel(int x, int y, int width, int height, String title, Color background, Color foreground) {
JLabel label = createLabel(x, y, width, height, title, background);
label.setForeground(foreground);
return label;
}
public static JLabel createLabel(int x, int y, int width, int height, String title, Color background) {
JLabel label = createLabel(x, y, width, height, title);
label.setBackground(background);
return label;
}
public static JLabel createLabel(int x, int y, int width, int height, String title) {
JLabel label = new JLabel();
if (title != null || title != "")
label.setText(title);
label.setBounds(x, y, width, height);
return label;
}
public static JMenu createMenu(String name, JMenuBar parent, MenuListener listener) {
JMenu menu = new JMenu(name);
menu.addMenuListener(listener);
parent.add(menu);
return menu;
}
public static JMenu createMenu(String name, JMenuBar parent) {
return createMenu(name, parent, null);
}
public static JMenuItem createMenuItem(String name, JMenu parent, ActionListener listener) {
JMenuItem item = new JMenuItem(name);
item.addActionListener(listener);
parent.add(item);
return item;
}
/**
* @return the defaultfont
*/
public static Font getDefaultfont() {
return defaultFont;
}
}
答案 0 :(得分:0)
您应该使用布局等容器并向其添加元素。这可以确保元素是固定的。如果你没有使用任何容器,那么当你调整所有元素时,不会随之调整大小。这也是问题所在。当您的标签尺寸发生变化时,整个内容都会受到影响。
只需查看此链接即可了解有关学习布局的更多信息。