我必须更改在 Swing 中实现的程序 GUI。现在我在单独的窗口中有许多图表,我想更改它,以便它们都只显示在一个图表中。原始版本的收缩是JFrame有一个JPanel字段。JFrame没有固定的布局。
private void initComponents() {// GEN-BEGIN:initComponents
getContentPane().setLayout(null);
setTitle("Wykres");
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
resizedWindow(evt);
}
});
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width - MIN_WIDTH) / 2, (screenSize.height - MIN_HEIGHT) / 2, MIN_WIDTH, MIN_HEIGHT);
}
JPanel声明如下:
BorderLayout borderLayout = new BorderLayout();
borderLayout.setHgap(0);
borderLayout.setVgap(0);
drawPanel = new JDrawPanel();
drawPanel.setLayout(borderLayout);
drawPanel.setComponentOrientation(ComponentOrientation.UNKNOWN);
setContentPane(drawPanel);
如果我删除 JFrame 的继承并添加 JPanel 的继承,则不会调用方法 paintComponent。我认为原因是布局设置不好。但是程序计算与边缘的距离,我不想更改它。如何轻松编辑此程序?
我还在 NETBEANS 中生成了新的 GUI
public class ViewPanel extends javax.swing.JFrame {
public ViewPanel() {
// initComponents();
}
public ViewPanel(JPanel histo, JPanel dystrybu, JPanel time){
initComponents(histo, dystrybu, time);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents(JPanel histo, JPanel dystrybu, JPanel time) {
MainTabs = new javax.swing.JTabbedPane();
jHistoTab = histo;
jDystybuTab = dystrybu;
jTimeTab = time;
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("zaleznosc czasowa");
MainTabs.setToolTipText("");
MainTabs.addTab("histogram", jHistoTab);
MainTabs.addTab("dystrybuanta", jDystybuTab);
MainTabs.addTab("zalenosc czasowa", jTimeTab);
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()
.addComponent(MainTabs, javax.swing.GroupLayout.DEFAULT_SIZE, 712, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(MainTabs, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
.addContainerGap())
);
MainTabs.getAccessibleContext().setAccessibleName("");
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ViewPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ViewPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ViewPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ViewPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ViewPanel().setVisible(true);
AppSMO.main(null);
}
});
}
public void setHistoTab(JPanel panel){
jHistoTab=panel;
}
// Variables declaration - do not modify
private javax.swing.JTabbedPane MainTabs;
private JPanel jDystybuTab;
private JPanel jHistoTab;
private JPanel jTimeTab;
// End of variables declaration
}
将边框布局设置为内容窗格。将面板添加到内容窗格。这将给它中心约束,以便它适应窗口的大小。在面板中,您可以自由添加所需的任何组件。我建议您不要在任何地方使用空布局。避免绝对定位。