我试图测试->this answer <-中提出的技术。
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
BufferedImage img = new BufferedImage(plotPanel.getWidth(),plotPanel.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.red);
g2d.drawLine(0, 0, plotPanel.getWidth(), plotPanel.getHeight());
JLabel picLabel = new JLabel(new ImageIcon( img ));
plotPanel.add(picLabel);
plotPanel.revalidate();
plotPanel.repaint();
}
为什么plotPanel仍然保持不变?
更新
以下是备受期待的SSCCE:
package javaapplication10;
import javax.swing.SwingUtilities;
public class JavaApplication10 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new NewJFrame().setVisible(true);
}
});
}
}
package javaapplication10;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
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() {
plotPanel = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
plotPanel.setBackground(new java.awt.Color(255, 255, 255));
javax.swing.GroupLayout plotPanelLayout = new javax.swing.GroupLayout(plotPanel);
plotPanel.setLayout(plotPanelLayout);
plotPanelLayout.setHorizontalGroup(
plotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
plotPanelLayout.setVerticalGroup(
plotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 379, Short.MAX_VALUE)
);
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(plotPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 758, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(plotPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addContainerGap(124, Short.MAX_VALUE))
);
plotPanel.getAccessibleContext().setAccessibleName("plotPanel");
plotPanel.getAccessibleContext().setAccessibleDescription("");
pack();
}// </editor-fold>
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
BufferedImage img = new BufferedImage(plotPanel.getWidth(),plotPanel.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.red);
g2d.drawLine(0, 0, plotPanel.getWidth(), plotPanel.getHeight());
JLabel picLabel = new JLabel(new ImageIcon( img ));
plotPanel.add(picLabel);
plotPanel.revalidate();
plotPanel.repaint();
}
/**
* @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);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JPanel plotPanel;
}
答案 0 :(得分:2)
确保在添加revalidate
JLabel
后致电picLabel
:
plotPanel.revalidate();
此外,您正在使用GroupLayout
,这需要将水平和垂直顺序组添加到布局中,以便可见任何新组件。这种布局不是最容易手动编码并产生大量的锅炉板代码。使用更传统的布局管理器熟悉代码。见A Visual Guide to Layout Managers
答案 1 :(得分:2)
您应该创建并使用自定义glass pane:
,而不是在没有适当约束的情况下添加组件。class NewJFrame extends JFrame {
// ...
class RedLineGlassPane extends JComponent {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
java.awt.Component contentPane = NewJFrame.this.getContentPane();
g2d.drawLine(0, 0,
contentPane.getWidth(), contentPane.getHeight());
}
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
setGlassPane(new RedLineGlassPane());
getGlassPane().setVisible(true);
repaint();
}
}
GroupLayout
生成非常混乱的代码。plotPanel
很好。 jButton1
不是。JavaApplication10
课程,因为NewJFrame
有main
方法GroupLayout
代码答案 2 :(得分:2)
您的plotPanel正在使用GroupLayout:
plotPanel = new javax.swing.JPanel();
javax.swing.GroupLayout plotPanelLayout = new javax.swing.GroupLayout(plotPanel);
plotPanel.setLayout(plotPanelLayout);
您不能在不指定约束的情况下将组件添加到GroupLayout:
plotPanel.add(picLabel);
我不知道GroupLayout是如何工作的,所以我建议您在GUI构建器中创建JLabel并将其添加到plotPanel中。也就是说,让GUI构建器为您创建所有约束。
然后,当您想要将Icon实际添加到标签中时:
picLabel.setIcon(new ImageIcon( img ));
现在,布局管理器将自动布局面板以包含新图像。
答案 3 :(得分:0)
主要问题是在使用布局管理器时,不能简单地添加元素,尤其是GroupLayout
。这是完整的解决方案,基于camickr和Reimeus技巧以及我的进一步调查:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
BufferedImage img = new BufferedImage(plotPanel.getWidth(),plotPanel.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.red);
g2d.drawLine(0, 0, plotPanel.getWidth(), plotPanel.getHeight());
jLabel1.setIcon(new ImageIcon(img)); //placed on plotPanel
plotPanel.add(jLabel1);
plotPanel.revalidate();
plotPanel.repaint();
}
总而言之,我们只需要关闭布局管理器:
Windows-&gt;导航 - &gt; Navigator-&gt; [JFrame] - &gt; plotPanel(右键单击) - &gt;设置布局 - &gt; null