我在 Java GUI 方面很糟糕,并使用生成器为我制作了大部分 GUI。我在 Netbeans 中制作了它,然后将其复制到 Eclipse。它在启动时比我功能强大的计算机延迟了大约 5 秒,尽管我已经尝试过计时它和其他东西,但我不知道为什么。我不想责怪生成器,但它 100% 来自那个 UI 代码。
我非常感谢有人帮助我找出导致它的原因,并总体上改进了丑陋的 UI 代码。其他代码未完成,这只是接口部分。
请注意,您可以删除我的所有代码,甚至是 gridLayout,它仍然会滞后,如果我将其编译为 .jar 并使用 -classpath 参数进行编译,它具有相同的滞后
package ...
imports ...
public class x extends javax.swing.JFrame {
public x() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point centrePoint = new Point(screenSize.width / 2, screenSize.height / 2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
schMon = new javax.swing.JLabel();
schTue = new javax.swing.JLabel();
schWed = new javax.swing.JLabel();
schThu = new javax.swing.JLabel();
schFri = new javax.swing.JLabel();
schSat = new javax.swing.JLabel();
schSun = new javax.swing.JLabel();
timeLabel = new javax.swing.JLabel();
schedulePanel = new javax.swing.JPanel();
holdingPanel = new javax.swing.JPanel();
isEnabled = new javax.swing.JCheckBox();
dropboxPath = new javax.swing.JTextField();
fileButton = new javax.swing.JButton();
saveButton = new javax.swing.JButton();
resetButton = new javax.swing.JButton();
sep2 = new javax.swing.JSeparator();
sep = new javax.swing.JSeparator();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
schMon.setText("Monday");
schTue.setText("Tuesday");
schWed.setText("Wednesday ");
schThu.setText("Thursday");
schFri.setText("Friday");
schSat.setText("Saturday");
schSun.setText("Sunday");
Font font = new Font("Calibri", Font.BOLD, 13);
timeLabel.setFont(font);
timeLabel.setText("");
schedulePanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
schedulePanel.setLayout(new GridLayout(7, 24, -1, -1));
schedulePanel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
//JPanel clickedPanel = (JPanel) getComponentAt(mouseEvent.getPoint());
// System.out.println(clickedPanel.getName().toString());
}
});
ArrayList<ArrayList<JPanel>> times = new ArrayList<ArrayList<JPanel>>();
for (int day = 0; day < 7; day++) {
times.add(new ArrayList<JPanel>());
for (int hour = 0; hour < 24; hour++) {
times.get(day).add(new JPanel());
JPanel current = times.get(day).get(hour);
current.setSize(42, 42);
current.setBorder(BorderFactory.createLineBorder(Color.black));
current.setMaximumSize(current.getSize());
current.setBackground(Color.LIGHT_GRAY);
schedulePanel.add(current);
}
}
holdingPanel.setPreferredSize(new java.awt.Dimension(2, 100));
isEnabled.setText("Enable Scheduler");
dropboxPath.setText("Path to your Dropbox.exe");
dropboxPath.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
fileButton.setText("Find");
fileButton.setPreferredSize(new java.awt.Dimension(60, 22));
saveButton.setText("Save");
resetButton.setText("Reset");
resetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
sep2.setOrientation(javax.swing.SwingConstants.VERTICAL);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
holdingPanel);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(isEnabled)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(dropboxPath, GroupLayout.PREFERRED_SIZE, 480, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(fileButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sep2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(58)
.addComponent(saveButton)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(resetButton)
.addGap(2))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(Alignment.TRAILING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(Alignment.BASELINE)
.addComponent(isEnabled, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sep2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(dropboxPath, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(fileButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(saveButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(resetButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
holdingPanel.setLayout(jPanel2Layout);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sep)
.addComponent(
holdingPanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
630, Short.MAX_VALUE)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
schSat)
.addComponent(
schSun)
.addComponent(
timeLabel)
.addComponent(
schMon)
.addComponent(
schTue)
.addComponent(
schWed)
.addComponent(
schThu)
.addComponent(
schFri))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
schedulePanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)))
.addContainerGap()));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(10)
.addComponent(
schMon)
.addGap(30)
.addComponent(
schTue)
.addGap(30)
.addComponent(
schWed)
.addGap(30)
.addComponent(
schThu)
.addGap(30)
.addComponent(
schFri)
.addGap(30)
.addComponent(
schSat)
.addGap(30)
.addComponent(
schSun)
.addGap(12)
.addComponent(
timeLabel)
.addGap(0,
0,
Short.MAX_VALUE))
.addComponent(
schedulePanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGap(10, 10, 10)
.addComponent(sep,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1)
.addComponent(holdingPanel,
javax.swing.GroupLayout.PREFERRED_SIZE,
45,
javax.swing.GroupLayout.PREFERRED_SIZE)));
pack();
Point newLocation = new Point(centrePoint.x - (this.getWidth() / 2), centrePoint.y - (this.getHeight() / 2));
setLocation(newLocation);
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
}
public static void main(String args[]) {
try {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new x().setVisible(true);
}
});
}
private javax.swing.JButton fileButton;
private javax.swing.JButton saveButton;
private javax.swing.JButton resetButton;
private javax.swing.JCheckBox isEnabled;
private javax.swing.JLabel schMon;
private javax.swing.JLabel schTue;
private javax.swing.JLabel schWed;
private javax.swing.JLabel schThu;
private javax.swing.JLabel schFri;
private javax.swing.JLabel schSat;
private javax.swing.JLabel schSun;
private javax.swing.JLabel timeLabel;
private javax.swing.JPanel holdingPanel;
private javax.swing.JSeparator sep;
private javax.swing.JSeparator sep2;
private javax.swing.JTextField dropboxPath;
private javax.swing.JPanel schedulePanel;
}