注意:我提供了一个完整的代码示例,以使其可由任何用户执行;我真正的代码很长!
我正在获得此例外情况:
navigationController
我正在处理执行任务的线程..像这样
我需要在事件发生时将一个Panel放在其他新面板中
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException:
javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,
alignmentX=0.0, alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@24dab6,
flags=9,maximumSize=,minimumSize=,preferredSize=] is not attached to a horizontal group
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException:
javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,
alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@bab939,
flags=9,maximumSize=,minimumSize=,preferredSize=] is not attached to a vertical group
调整JFrame大小时,抛出componentResized .....
private void insertPanelInPanel(JPanel jpContainer, JPanel jpContained) {
GroupLayout jpContainerLayout = (GroupLayout)jpContainer.getLayout();
jpContainer.removeAll();
//GroupLayout jpContainerLayout = new GroupLayout(jpContainer);
//jpContainer.setLayout(jpContainerLayout);
jpContainerLayout.setHorizontalGroup(
jpContainerLayout.createParallelGroup()
.addComponent(jpContained)
);
jpContainerLayout.setVerticalGroup(
jpContainerLayout.createParallelGroup()
.addComponent(jpContained)
);
}
输出
package test;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.Map.Entry;
import javax.swing.*;
public class NewJFrame extends javax.swing.JFrame {
Thread thrdOne = null;
Thread thrdTwo = null;
public NewJFrame() {
initComponents();
}
private boolean isRunningPlotThreads() {
boolean isRunning = false;
if (!(thrdOne == null)) {
if (thrdOne.getState() == Thread.State.TERMINATED) {
thrdOne = null;
} else {
isRunning = true;
}
}
if (!(thrdTwo == null)) {
if (thrdTwo.getState() == Thread.State.TERMINATED) {
thrdTwo = null;
} else {
isRunning = true;
}
}
return isRunning;
}
private void insertPanelInPanel(JPanel jpContainer, JPanel jpContained) {
GroupLayout jpContainerLayout = (GroupLayout)jpContainer.getLayout();
jpContainer.removeAll();
// GroupLayout jpContainerLayout = new GroupLayout(jpContainer);
// jpContainer.setLayout(jpContainerLayout);
jpContainerLayout.setHorizontalGroup(
jpContainerLayout.createParallelGroup()
.addComponent(jpContained)
);
jpContainerLayout.setVerticalGroup(
jpContainerLayout.createParallelGroup()
.addComponent(jpContained)
);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jtpBot = new javax.swing.JTabbedPane();
jpOne = new javax.swing.JPanel();
jbFirst = new javax.swing.JButton();
jpOnePlot = new javax.swing.JPanel();
jpTwo = new javax.swing.JPanel();
jbTwo = new javax.swing.JButton();
jpTwoPlot = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
formComponentResized(evt);
}
});
jtpBot.setBackground(new java.awt.Color(0, 0, 0));
jtpBot.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jpOne.setBackground(new java.awt.Color(0, 191, 191));
jbFirst.setText("First");
jbFirst.setMaximumSize(new java.awt.Dimension(70, 23));
jbFirst.setMinimumSize(new java.awt.Dimension(70, 23));
jbFirst.setPreferredSize(new java.awt.Dimension(70, 23));
jbFirst.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbFirstActionPerformed(evt);
}
});
jpOnePlot.setBackground(new java.awt.Color(191, 191, 191));
jpOnePlot.setBorder(javax.swing.BorderFactory.createEtchedBorder());
javax.swing.GroupLayout jpOnePlotLayout = new javax.swing.GroupLayout(jpOnePlot);
jpOnePlot.setLayout(jpOnePlotLayout);
jpOnePlotLayout.setHorizontalGroup(
jpOnePlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 291, Short.MAX_VALUE)
);
jpOnePlotLayout.setVerticalGroup(
jpOnePlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
javax.swing.GroupLayout jpOneLayout = new javax.swing.GroupLayout(jpOne);
jpOne.setLayout(jpOneLayout);
jpOneLayout.setHorizontalGroup(
jpOneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jpOneLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jbFirst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jpOnePlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jpOneLayout.setVerticalGroup(
jpOneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jpOneLayout.createSequentialGroup()
.addContainerGap()
.addGroup(jpOneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jpOnePlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jpOneLayout.createSequentialGroup()
.addComponent(jbFirst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 143, Short.MAX_VALUE)))
.addContainerGap())
);
jtpBot.addTab("One", jpOne);
jpTwo.setBackground(new java.awt.Color(191, 0, 191));
jbTwo.setText("Two");
jbTwo.setMaximumSize(new java.awt.Dimension(70, 23));
jbTwo.setMinimumSize(new java.awt.Dimension(70, 23));
jbTwo.setPreferredSize(new java.awt.Dimension(70, 23));
jbTwo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbTwoActionPerformed(evt);
}
});
jpTwoPlot.setBackground(new java.awt.Color(191, 191, 191));
jpTwoPlot.setBorder(javax.swing.BorderFactory.createEtchedBorder());
javax.swing.GroupLayout jpTwoPlotLayout = new javax.swing.GroupLayout(jpTwoPlot);
jpTwoPlot.setLayout(jpTwoPlotLayout);
jpTwoPlotLayout.setHorizontalGroup(
jpTwoPlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 291, Short.MAX_VALUE)
);
jpTwoPlotLayout.setVerticalGroup(
jpTwoPlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
javax.swing.GroupLayout jpTwoLayout = new javax.swing.GroupLayout(jpTwo);
jpTwo.setLayout(jpTwoLayout);
jpTwoLayout.setHorizontalGroup(
jpTwoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jpTwoLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jbTwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jpTwoPlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jpTwoLayout.setVerticalGroup(
jpTwoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jpTwoLayout.createSequentialGroup()
.addContainerGap()
.addGroup(jpTwoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jpTwoPlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jpTwoLayout.createSequentialGroup()
.addComponent(jbTwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 143, Short.MAX_VALUE)))
.addContainerGap())
);
jtpBot.addTab("Two", jpTwo);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jtpBot)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(69, 69, 69)
.addComponent(jtpBot)
.addContainerGap())
);
pack();
}// </editor-fold>
private void formComponentResized(java.awt.event.ComponentEvent evt) {
if (jtpBot.getSelectedIndex() == jtpBot.indexOfTab("One")) {
if (jbFirst.isEnabled()) {
jbFirstActionPerformed(null);
}
}
if (jtpBot.getSelectedIndex() == jtpBot.indexOfTab("Two")) {
if (jbTwo.isEnabled()) {
jbTwoActionPerformed(null);
}
}
}
private void jbFirstActionPerformed(java.awt.event.ActionEvent evt) {
try {
System.out.println("jbFirstActionPerformed");
final JPanel jpOneNew = new JPanel();
int c = 32+(int)(208*Math.random());
jpOneNew.setBackground(new Color(c,0,0));
Runnable plotOne = new Runnable() {
public void run() {
try {
Thread.sleep((int)(500*Math.random())); //simulate Time Proccesing
insertPanelInPanel(jpOnePlot, jpOneNew);
} catch (Exception ioe) {
System.out.println("-insertPanelInPanel(jpOnePlot, jpOneNew):"+ioe.toString());
}
}
};
if (!isRunningPlotThreads()) {
thrdOne = new Thread(plotOne);
thrdOne.start();
}
} catch (Exception ioe) {
System.out.println("jpOneNew:"+ioe.toString());
}
}
private void jbTwoActionPerformed(java.awt.event.ActionEvent evt) {
try {
System.out.println("jbTwoActionPerformed");
final JPanel jpTwoNew = new JPanel();
int c = 32+(int)(208*Math.random());
jpTwoNew.setBackground(new Color(0,c,0));
Runnable plotTwo = new Runnable() {
public void run() {
try {
Thread.sleep((int)(500*Math.random())); //simulate Time Proccesing
insertPanelInPanel(jpTwoPlot, jpTwoNew);
} catch (Exception ioe) {
System.out.println("-insertPanelInPanel(jpTwoPlot, jpTwoNew):"+ioe.toString());
}
}
};
if (!isRunningPlotThreads()) {
thrdTwo = new Thread(plotTwo);
thrdTwo.start();
}
} catch (Exception ioe) {
System.out.println("jpTwoNew:"+ioe.toString());
}
}
public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
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>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
boolean bCanIuseOne = false;
boolean bCanIuseTwo = false;
boolean bThreadFree = true;
boolean bMouseFree = false;
private javax.swing.JButton jbFirst;
private javax.swing.JButton jbTwo;
private javax.swing.JPanel jpOne;
private javax.swing.JPanel jpOnePlot;
private javax.swing.JPanel jpTwo;
private javax.swing.JPanel jpTwoPlot;
private javax.swing.JTabbedPane jtpBot;
}
问题
出了什么问题?
如何防止抛出异常?
我如何能够抓住这些例外情况?
答案 0 :(得分:2)
试试这个!!!
private void insertPanelInPanel(JPanel jpContainer, JPanel jpContained) {
jpContainer.removeAll();
jpContainer.setLayout(new java.awt.GridLayout(1,1));
jpContainer.add(jpContained);
}