编辑: 当然我在发帖后2分钟找到答案。 How to completely remove an icon from JDialog?
但后续行动;任何人都知道在更改可调整大小的属性时如何设置一致的图标状态?
PS。我再也不能回答8个小时了。
原始问题: 简而言之,这是一个有问题的精简示例代码。 当我点击“详细信息”(WinXP x32 java 1.6)时,jPanel2会更改可见性状态,并且由于某种原因,jPanel2会同时删除对话框图标。非常好奇。 任何人都知道为什么图标与JPanel一起隐藏? 代码由NetBeans 7.1生成,只是略微适应自包含。
提前感谢您的帮助!
import java.awt.Frame;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class TestHide extends javax.swing.JDialog {
private static final long serialVersionUID = 1L;
public TestHide(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
jPanel2.setVisible(false);
pack();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
lblIcon = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
taShortMsg = new javax.swing.JTextArea();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jSeparator2 = new javax.swing.JSeparator();
jPanel4 = new javax.swing.JPanel();
jSeparator3 = new javax.swing.JSeparator();
jScrollPane2 = new javax.swing.JScrollPane();
taDetails = new javax.swing.JTextArea();
btnOk = new javax.swing.JButton();
btnTgDetails = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
lblIcon.setText(""); // NOI18N
jScrollPane1.setBorder(null);
taShortMsg.setBackground(getBackground());
taShortMsg.setColumns(20);
taShortMsg.setEditable(false);
taShortMsg.setFont(lblIcon.getFont());
taShortMsg.setLineWrap(true);
taShortMsg.setRows(5);
taShortMsg.setBorder(null);
taShortMsg.setCursor(getCursor());
taShortMsg.setOpaque(false);
jScrollPane1.setViewportView(taShortMsg);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(lblIcon).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE).addContainerGap()));
jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel1Layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup().addComponent(lblIcon).addContainerGap(54, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING))));
jPanel2.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentHidden(java.awt.event.ComponentEvent evt) {
jPanel2ComponentHidden(evt);
}
public void componentShown(java.awt.event.ComponentEvent evt) {
jPanel2ComponentShown(evt);
}
});
jLabel3.setText("jLabel3"); // NOI18N
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel3Layout.createSequentialGroup().addContainerGap().addComponent(jSeparator1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jSeparator2).addContainerGap()));
jPanel3Layout
.setVerticalGroup(jPanel3Layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
jPanel3Layout
.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(
jPanel3Layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 10,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 10,
javax.swing.GroupLayout.PREFERRED_SIZE))).addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE));
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel4Layout.createSequentialGroup().addContainerGap().addComponent(jSeparator3).addContainerGap()));
jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel4Layout.createSequentialGroup().addContainerGap().addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)));
jScrollPane2.setBorder(null);
taDetails.setColumns(20);
taDetails.setEditable(false);
taDetails.setFont(lblIcon.getFont());
taDetails.setRows(5);
jScrollPane2.setViewportView(taDetails);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane2).addContainerGap())
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
jPanel2Layout.createSequentialGroup()
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)));
btnOk.setText("OK"); // NOI18N
btnOk.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnOkActionPerformed(evt);
}
});
btnTgDetails.setText("Details"); // NOI18N
btnTgDetails.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnTgDetailsActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(
layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnTgDetails, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap())
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout
.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(btnTgDetails)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(btnOk).addContainerGap()))));
pack();
}
private void btnTgDetailsActionPerformed(java.awt.event.ActionEvent evt) {
if (jPanel2.isVisible()) {
btnTgDetails.setText("Details>>");
} else {
btnTgDetails.setText("Details<<");
}
jPanel2.setVisible(!jPanel2.isVisible());
this.validate();
this.pack();
}
private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
private void jPanel2ComponentHidden(java.awt.event.ComponentEvent evt) {
setResizable(false);
}
private void jPanel2ComponentShown(java.awt.event.ComponentEvent evt) {
setResizable(true);
}
public static void main(String args[]) {
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(TestHide.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestHide.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestHide.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestHide.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
showMessageDialog(null, "I am title", "Small message...", "long\n\nlong\nmessage", JOptionPane.INFORMATION_MESSAGE);
}
public static void showMessageDialog(Frame parent, String title, String shortMessage, String longMessage, int type) {
TestHide md = new TestHide(parent, true);
Icon icon = null;
switch (type) {
case JOptionPane.ERROR_MESSAGE:
icon = UIManager.getIcon("OptionPane.errorIcon");
break;
case JOptionPane.INFORMATION_MESSAGE:
icon = UIManager.getIcon("OptionPane.informationIcon");
break;
case JOptionPane.WARNING_MESSAGE:
icon = UIManager.getIcon("OptionPane.warningIcon");
break;
}
md.setTitle(title);
md.lblIcon.setIcon(icon);
md.taShortMsg.setText(shortMessage);
if (longMessage == null) {
md.btnTgDetails.setVisible(false);
} else {
md.taDetails.setText(longMessage);
md.taDetails.setCaretPosition(0);
}
md.pack();
md.setVisible(true);
}
private javax.swing.JButton btnOk;
private javax.swing.JToggleButton btnTgDetails;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JLabel lblIcon;
private javax.swing.JTextArea taDetails;
private javax.swing.JTextArea taShortMsg;
}
答案 0 :(得分:0)
如果将以下语句放在
之类的构造函数中public TestHide(java.awt.Frame parent, boolean modal) {
super(parent, modal);
setIconImage(Toolkit.getDefaultToolkit().getImage("images\\white_title.png"));
initComponents();
jPanel2.setVisible(false);
pack(); }
然后标题栏上的图标不断显示。