0
我剛剛在我的GUI中添加了徽標和測試版消息。然而,它正確地完全不是那麼回事中心:Swing Component Not Centering
這裏是我的代碼:
package me.nrubin29.quiz.create;
import me.nrubin29.quiz.MessageUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class WelcomeGUI extends JFrame {
private static final long serialVersionUID = 1L;
private DefaultListModel contents = new DefaultListModel();
private JLabel logo, betaText = new JLabel("This is beta software and should not be distributed without consent from its creator.");
private JList list = new JList(contents);
private JPanel contentsPanel = new JPanel(), buttonPanel = new JPanel(), logoPanel = new JPanel(), betaPanel = new JPanel();
private JButton open = new JButton("Open"), delete = new JButton("Delete"), rename = new JButton("Rename"), newQuiz = new JButton("New");
public WelcomeGUI() {
super("AutoQuiz");
try { logo = new JLabel(MessageUtil.ImageType.LOGO_LANDSCAPE.getImageIcon()); }
catch (Exception e) { e.printStackTrace(); }
logo.setHorizontalAlignment(SwingConstants.CENTER);
betaText.setHorizontalAlignment(SwingConstants.CENTER);
list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() == 2) open.doClick();
}
});
list.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) open.doClick();
}
});
for (String file : FileManager.getInstance().getRootFolder().list()) {
try { if (file.substring(file.lastIndexOf(".")).equals(".quiz")) contents.addElement(file.substring(0, file.lastIndexOf("."))); }
catch (Exception ignored) { }
}
if (contents.size() > 0) list.setSelectedIndex(0);
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (list.getSelectedIndex() != -1) {
File load = FileManager.getInstance().getFile(contents.get(list.getSelectedIndex()) + ".quiz");
new CreatorGUI(load, WelcomeGUI.this);
}
}
});
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (list.getSelectedIndex() != -1) {
File choice = FileManager.getInstance().getFile(contents.get(list.getSelectedIndex()) + ".quiz");
contents.remove(list.getSelectedIndex());
if (!choice.delete()) MessageUtil.msg(WelcomeGUI.this, "Error", "Could not delete file.", MessageUtil.MessageType.ERROR, MessageUtil.ImageType.ICON);
}
}
});
rename.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (list.getSelectedIndex() != -1) {
File load = FileManager.getInstance().getFile(contents.get(list.getSelectedIndex()) + ".quiz");
String name = JOptionPane.showInputDialog(WelcomeGUI.this, "Please enter a new name.", contents.get(list.getSelectedIndex()));
if (name == null) return;
if (!load.renameTo(FileManager.getInstance().getFile(name + ".quiz"))) MessageUtil.msg(WelcomeGUI.this, "Error", "Could not rename file.", MessageUtil.MessageType.ERROR, MessageUtil.ImageType.ICON);
else contents.setElementAt(name, list.getSelectedIndex());
}
}
});
newQuiz.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = JOptionPane.showInputDialog(WelcomeGUI.this, "Please enter a name for the quiz.");
if (name == null) return;
else name += ".quiz";
new CreatorGUI(FileManager.getInstance().getFile(name), WelcomeGUI.this);
WelcomeGUI.this.dispose();
}
});
JScrollPane listPane = new JScrollPane(list);
listPane.setPreferredSize(new Dimension(500, 125));
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
logoPanel.setLayout(new BoxLayout(logoPanel, BoxLayout.Y_AXIS));
logoPanel.add(logo);
buttonPanel.add(open); buttonPanel.add(delete); buttonPanel.add(rename); buttonPanel.add(newQuiz);
contentsPanel.add(listPane); contentsPanel.add(buttonPanel);
betaPanel.add(betaText);
add(logoPanel); add(contentsPanel); add(betaPanel);
Dimension DIM = new Dimension(640, 250);
setPreferredSize(DIM);
setSize(DIM);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
pack();
setVisible(true);
}
public static void main(String[] args) {
new WelcomeGUI();
}
}
如果你徽標JLabel的MAXIMUMSIZE設置爲大寬度,也許'Byte.MAX_VALUE'。 –