我正在尝试使用Java编写程序。它正在使用Jmenu和Jmenu项目。我的目标是在外部课程上为每个Jmenu项目设置一个Jpanel。 Netbeans帮助我制作菜单的基础...但我不明白我如何使用外部类的Jpanel。我搜索了一些例子,但我找不到可以帮助我或看起来像我的问题的那些。
感谢您的帮助。
Jframe:
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jMenu1.setText("Accueil");
jMenuBar1.add(jMenu1);
jMenu2.setText("Caisse");
jMenuItem1.setText("Ouvrir Caisse");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem1);
jMenuBar1.add(jMenu2);
jMenu3.setText("Consultation");
jMenuItem2.setText("Consulter BD");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu3.add(jMenuItem2);
jMenuBar1.add(jMenu3);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 455, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 311, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
//Jpanel "PanelCaisse"
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
//Jpanel "PanelConsult"
}
/**
* @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private JMenu jMenu1;
private JMenu jMenu2;
private JMenu jMenu3;
private JMenuBar jMenuBar1;
private JMenuItem jMenuItem1;
private JMenuItem jMenuItem2;
// End of variables declaration
private static class label {
private static void setText(String mode_Caisse) {
throw new UnsupportedOperationException("Not supported yet.");
}
public label() {
}
}
}
小组“Caisse”(供测试)
import java.awt.Graphics;
import javax.swing.JPanel;
public class PanelCaisse extends JPanel {
public void paintComponent(Graphics g){
int x1 = this.getWidth()/4;
int y1 = this.getHeight()/4;
g.fillOval(x1, y1, this.getWidth()/2, this.getHeight()/2);
}
}
小组“咨询”
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PanelConsult extends javax.swing.JPanel {
private JTextField jTextField1;
private JButton jButton1;
private JLabel jLabel1;
private JPanel panelc = new JPanel();
public void ConsultBD() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel1.setText("Votre recherche");
jButton1.setText("Ok");
jTextField1.setText("Recherche");
GroupLayout layout = new GroupLayout(panelc);
panelc.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addContainerGap(192, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jButton1))
.addContainerGap(277, Short.MAX_VALUE))
);
}
}
答案 0 :(得分:0)
这是一个功能演示,演示如何在您的问题上应用CardLayout
:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CardLayoutDemo {
JFrame frame = new JFrame();
JMenuBar menuBar = new JMenuBar();
JMenu mnuPanels = new JMenu("Panels");
JMenuItem mniFirstPanel = new JMenuItem("First panel");
JMenuItem mniSecondPanel = new JMenuItem("Second panel");
JPanel panel = new JPanel();
JPanel pnlFirst = new JPanel();
JPanel pnlSecond = new JPanel();
CardLayout cardLayout = new CardLayout();
public CardLayoutDemo() {
panel.setLayout(cardLayout);
pnlFirst.add(new JLabel("This is first panel"));
pnlSecond.add(new JLabel("This is second panel"));
//Add panels and set constraints.
panel.add(pnlFirst, "pnlFirst");
panel.add(pnlSecond, "pnlSecond");
//Shows first layout on startup of application.
/*Pass parent container (where you set CardLayout) and
* constraint of panel you would like to show.
* */
cardLayout.show(panel, "pnlFirst");
//Add menu and menu items.
menuBar.add(mnuPanels);
mnuPanels.add(mniFirstPanel);
mnuPanels.add(mniSecondPanel);
mniFirstPanel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//Shows pnlFirst
cardLayout.show(panel, "pnlFirst");
}
});
mniSecondPanel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//Shows pnlSecond
cardLayout.show(panel, "pnlSecond");
}
});
frame.setJMenuBar(menuBar);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(640, 480);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CardLayoutDemo();
}
});
}
}
使用CardLayout
的想法是完美的,但我无法告诉您如何使用NetBeans GUI构建器实现此目的,因为我手动编码。你应该这样做。