gpt4 book ai didi

带有 NetBeans 的 Java GUI

转载 作者:行者123 更新时间:2023-12-01 07:37:20 27 4
gpt4 key购买 nike

我正在尝试在 GUI 中制作图表,但我真的不明白它是如何工作的。我可以使用 netbeans 拖放来实现视觉效果,但是当涉及到编码时我不明白。

在我制作出事物的视觉方面后,如何将 Action /事件等事物组合在一起。这是我的设计代码。

package my.mathamaticsui;

public class MathamaticsUI extends javax.swing.JFrame {

/**
* Creates new form MathamaticsUI
*/
public MathamaticsUI() {
initComponents();
}

/**
* 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() {

jPanel1 = new javax.swing.JPanel();
jScrollPane5 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jTabbedPane1 = new javax.swing.JTabbedPane();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea3 = new javax.swing.JTextArea();
jScrollPane4 = new javax.swing.JScrollPane();
jTextArea4 = new javax.swing.JTextArea();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTabbedPane2 = new javax.swing.JTabbedPane();
jLabel2 = new javax.swing.JLabel();

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);

jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane5.setViewportView(jList1);

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTabbedPane1.setBackground(new java.awt.Color(204, 204, 204));

jTextArea2.setBackground(new java.awt.Color(204, 204, 204));
jTextArea2.setColumns(20);
jTextArea2.setEditable(false);
jTextArea2.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
jTextArea2.setRows(5);
jTextArea2.setText("Polynominal Functions:\nf(x)= ax^3 + bx^2 + c\n\n");
jScrollPane2.setViewportView(jTextArea2);

jTabbedPane1.addTab("Polynominal", jScrollPane2);

jTextArea1.setBackground(new java.awt.Color(204, 204, 204));
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
jTextArea1.setRows(5);
jTextArea1.setText("Exponential Functions:\nf(x) = Ae^x\nf(x) = Ab^x");
jScrollPane1.setViewportView(jTextArea1);

jTabbedPane1.addTab("Exponential", jScrollPane1);

jTextArea3.setBackground(new java.awt.Color(204, 204, 204));
jTextArea3.setColumns(20);
jTextArea3.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
jTextArea3.setRows(5);
jTextArea3.setText("Trigonometric Functions:\nf(x) = AsinBx + c");
jScrollPane3.setViewportView(jTextArea3);

jTabbedPane1.addTab("Trigonometric", jScrollPane3);

jTextArea4.setBackground(new java.awt.Color(204, 204, 204));
jTextArea4.setColumns(20);
jTextArea4.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
jTextArea4.setRows(5);
jTextArea4.setText("Parametric Functions:\nf(t) = (AcosBt, AsinDt)\n");
jTextArea4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jScrollPane4.setViewportView(jTextArea4);

jTabbedPane1.addTab("Parametric", jScrollPane4);

jPanel2.addAncestorListener(new javax.swing.event.AncestorListener() {
public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
}
public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
jPanel2AncestorAdded(evt);
}
public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
}
});

jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
jLabel1.setText("Graph");

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jTabbedPane2)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);

jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
jLabel2.setText("Data");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(9, 9, 9)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jPanel2AncestorAdded(javax.swing.event.AncestorEvent evt) {
// TODO add your handling code here:
}

/**
* @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(MathamaticsUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MathamaticsUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MathamaticsUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MathamaticsUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new MathamaticsUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JList jList1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JScrollPane jScrollPane5;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTabbedPane jTabbedPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextArea jTextArea3;
private javax.swing.JTextArea jTextArea4;
// End of variables declaration
}

最佳答案

我建议从 Swing 教程开始:

http://docs.oracle.com/javase/tutorial/uiswing/TOC.html

注意:在 NetBeans 中,注册事件监听器

  • 点击组件
  • 在属性面板中:转到事件选项卡并注册名称
  • 转到代码 View 并添加监听器代码

关于带有 NetBeans 的 Java GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10163932/

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