我有一个swing应用程序,它的左侧有一个按钮列表,右侧有一个面板,就像gif一样。根据单击哪个按钮,将出现一个包含一些特定信息的新面板。该代码完全基于Netbeans 8.2默认的GUI编辑器:
import java.awt.Color;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
panelControl = new javax.swing.JPanel();
panelEncoder = new javax.swing.JPanel();
lblEncoder = new javax.swing.JLabel();
iconEncoder = new javax.swing.JLabel();
panelRoute = new javax.swing.JPanel();
lblRoute = new javax.swing.JLabel();
iconRoute = new javax.swing.JLabel();
panelLog = new javax.swing.JPanel();
lblLog = new javax.swing.JLabel();
iconLog = new javax.swing.JLabel();
panelOrder = new javax.swing.JPanel();
lblOrder = new javax.swing.JLabel();
iconOrder = new javax.swing.JLabel();
panelLogo = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
panelControl.setBackground(new java.awt.Color(255, 59, 0));
panelEncoder.setBackground(new java.awt.Color(255, 59, 0));
panelEncoder.setPreferredSize(new java.awt.Dimension(208, 58));
panelEncoder.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
panelEncoderMouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
panelEncoderMouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
panelEncoderMouseExited(evt);
}
});
lblEncoder.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
lblEncoder.setForeground(new java.awt.Color(255, 255, 255));
lblEncoder.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
lblEncoder.setText("A");
iconEncoder.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons8-mixer-de-configurações-vertical-32.png"))); // NOI18N
javax.swing.GroupLayout panelEncoderLayout = new javax.swing.GroupLayout(panelEncoder);
panelEncoder.setLayout(panelEncoderLayout);
panelEncoderLayout.setHorizontalGroup(
panelEncoderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelEncoderLayout.createSequentialGroup()
.addContainerGap()
.addComponent(iconEncoder)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblEncoder, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
.addContainerGap())
);
panelEncoderLayout.setVerticalGroup(
panelEncoderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelEncoderLayout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(panelEncoderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(lblEncoder, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
.addComponent(iconEncoder, javax.swing.GroupLayout.Alignment.LEADING))
.addContainerGap())
);
panelRoute.setBackground(new java.awt.Color(255, 59, 0));
panelRoute.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
panelRouteMouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
panelRouteMouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
panelRouteMouseExited(evt);
}
});
lblRoute.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
lblRoute.setForeground(new java.awt.Color(255, 255, 255));
lblRoute.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
lblRoute.setText("B");
iconRoute.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons8-rota-32.png"))); // NOI18N
javax.swing.GroupLayout panelRouteLayout = new javax.swing.GroupLayout(panelRoute);
panelRoute.setLayout(panelRouteLayout);
panelRouteLayout.setHorizontalGroup(
panelRouteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelRouteLayout.createSequentialGroup()
.addContainerGap()
.addComponent(iconRoute)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblRoute, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
panelRouteLayout.setVerticalGroup(
panelRouteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelRouteLayout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(panelRouteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblRoute, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(panelRouteLayout.createSequentialGroup()
.addComponent(iconRoute)
.addGap(0, 0, Short.MAX_VALUE)))
.addGap(10, 10, 10))
);
panelLog.setBackground(new java.awt.Color(255, 59, 0));
panelLog.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
panelLogMouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
panelLogMouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
panelLogMouseExited(evt);
}
});
lblLog.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
lblLog.setForeground(new java.awt.Color(255, 255, 255));
lblLog.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
lblLog.setText("D");
iconLog.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons8-relatório-de-sistema-32.png"))); // NOI18N
javax.swing.GroupLayout panelLogLayout = new javax.swing.GroupLayout(panelLog);
panelLog.setLayout(panelLogLayout);
panelLogLayout.setHorizontalGroup(
panelLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLogLayout.createSequentialGroup()
.addContainerGap()
.addComponent(iconLog)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblLog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
panelLogLayout.setVerticalGroup(
panelLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLogLayout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(panelLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(lblLog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(iconLog, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
panelOrder.setBackground(new java.awt.Color(255, 59, 0));
panelOrder.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
panelOrderMouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
panelOrderMouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
panelOrderMouseExited(evt);
}
});
lblOrder.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
lblOrder.setForeground(new java.awt.Color(255, 255, 255));
lblOrder.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
lblOrder.setText("C");
iconOrder.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons8-lista-de-tarefas-32.png"))); // NOI18N
javax.swing.GroupLayout panelOrderLayout = new javax.swing.GroupLayout(panelOrder);
panelOrder.setLayout(panelOrderLayout);
panelOrderLayout.setHorizontalGroup(
panelOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelOrderLayout.createSequentialGroup()
.addContainerGap()
.addComponent(iconOrder)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblOrder)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
panelOrderLayout.setVerticalGroup(
panelOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelOrderLayout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(panelOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblOrder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(panelOrderLayout.createSequentialGroup()
.addComponent(iconOrder)
.addGap(0, 0, Short.MAX_VALUE)))
.addGap(10, 10, 10))
);
javax.swing.GroupLayout panelControlLayout = new javax.swing.GroupLayout(panelControl);
panelControl.setLayout(panelControlLayout);
panelControlLayout.setHorizontalGroup(
panelControlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panelEncoder, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
.addComponent(panelRoute, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panelLog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panelOrder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
panelControlLayout.setVerticalGroup(
panelControlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelControlLayout.createSequentialGroup()
.addComponent(panelEncoder, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(panelRoute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(panelOrder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(panelLog, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
panelLogo.setBackground(new java.awt.Color(255, 255, 255));
javax.swing.GroupLayout panelLogoLayout = new javax.swing.GroupLayout(panelLogo);
panelLogo.setLayout(panelLogoLayout);
panelLogoLayout.setHorizontalGroup(
panelLogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
panelLogoLayout.setVerticalGroup(
panelLogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("This panel should change depending on the icon I click");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(93, 93, 93)
.addComponent(jLabel1)
.addContainerGap(107, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panelLogo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(panelControl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(panelLogo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panelControl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
getAccessibleContext().setAccessibleName("SparkAG\n");
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void panelOrderMouseExited(java.awt.event.MouseEvent evt) {
this.panelOrder.setBackground(new Color(255, 59, 0));
}
private void panelOrderMouseEntered(java.awt.event.MouseEvent evt) {
this.panelOrder.setBackground(new Color(255, 106, 0));
}
private void panelOrderMouseClicked(java.awt.event.MouseEvent evt) {
/*SwingUtilities.invokeLater(()
-> JOptionPane.showInputDialog(null, "Type here: "));*/
this.panelOrder.setBackground(new Color(255, 59, 0));
}
private void panelLogMouseExited(java.awt.event.MouseEvent evt) {
this.panelLog.setBackground(new Color(255, 59, 0));
}
private void panelLogMouseEntered(java.awt.event.MouseEvent evt) {
this.panelLog.setBackground(new Color(255, 106, 0));
}
private void panelLogMouseClicked(java.awt.event.MouseEvent evt) {
/*SwingUtilities.invokeLater(() ->
JOptionPane.showInputDialog(null, "Type here: "));*/
this.panelLog.setBackground(new Color(255, 59, 0));
}
private void panelRouteMouseExited(java.awt.event.MouseEvent evt) {
this.panelRoute.setBackground(new Color(255, 59, 0));
}
private void panelRouteMouseEntered(java.awt.event.MouseEvent evt) {
this.panelRoute.setBackground(new Color(255, 106, 0));
}
private void panelRouteMouseClicked(java.awt.event.MouseEvent evt) {
/*SwingUtilities.invokeLater(() ->
JOptionPane.showInputDialog(null, "Type here: "));*/
this.panelRoute.setBackground(new Color(255, 59, 0));
}
private void panelEncoderMouseExited(java.awt.event.MouseEvent evt) {
this.panelEncoder.setBackground(new Color(255, 59, 0));
}
private void panelEncoderMouseEntered(java.awt.event.MouseEvent evt) {
this.panelEncoder.setBackground(new Color(255, 106, 0));
}
private void panelEncoderMouseClicked(java.awt.event.MouseEvent evt) {
/*SwingUtilities.invokeLater(() ->
JOptionPane.showInputDialog(null, "Type here: "));*/
this.panelEncoder.setBackground(new Color(255, 59, 0));
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(() -> {
new NewJFrame().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JLabel iconEncoder;
private javax.swing.JLabel iconLog;
private javax.swing.JLabel iconOrder;
private javax.swing.JLabel iconRoute;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JLabel lblEncoder;
private javax.swing.JLabel lblLog;
private javax.swing.JLabel lblOrder;
private javax.swing.JLabel lblRoute;
private javax.swing.JPanel panelControl;
private javax.swing.JPanel panelEncoder;
private javax.swing.JPanel panelLog;
private javax.swing.JPanel panelLogo;
private javax.swing.JPanel panelOrder;
private javax.swing.JPanel panelRoute;
// End of variables declaration
}
我该怎么做才能根据单击的按钮显示其他面板?