我在使用Netbeans设计GUI(Yeah im lazy:\)并手动尝试在JFrame上绘制三角形时遇到了一些问题。 Swing组件被“掩盖”,直到我按Tab键并将焦点放在对象上。我附上了picture以及问题的下方代码。
GUI的所有自动生成代码都在代码的initComponents()部分中。 Triage生成在JFrame Paint方法的覆盖代码中。 它看起来正在发生的是initComponents代码在绘制之前运行,因为该对象是在setVisibile(true)之前创建的。调用setVisible(true)后,paint方法将绘制initComponents创建的所有生成对象。只是寻找一个解决方案,以免任何事情被掩盖。
任何帮助都将不胜感激。
$/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SimpleClient.java
*
* Created on Sep 22, 2011, 11:38:30 AM
*/
package Assignment3;
import java.awt.Graphics;
/**
*
* @author Mark
*/
public class SimpleClient extends javax.swing.JFrame {
/** Creates new form SimpleClient */
public SimpleClient() {
initComponents();
}
public void paint(Graphics g) {
int[] xPoints = {100, 100, 200};
int[] yPoints = {100, 200, 200};
g.drawPolygon(xPoints, yPoints, 3);
}
/** 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() {
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("jTextField1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(103, 103, 103)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(238, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(220, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(60, 60, 60))
);
pack();
}// </editor-fold>
/**
* @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(SimpleClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SimpleClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SimpleClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SimpleClient.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 SimpleClient().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
答案 0 :(得分:4)
不要覆盖顶级容器(JFrame,JDialog ...)的paint()方法。
通过覆盖JPanel(或JComponent)的paintCompnent()方法来完成自定义绘制。然后,将组件添加到框架的内容窗格中。不要忘记也覆盖组件的getPreferredSize()方法,以便布局管理器正常工作。
阅读Custom Painting以获取更多信息和工作示例。
答案 1 :(得分:4)
一些快速建议: