我对java非常精通。我试图在jpanel中画一条线(使用netbean ide)…我读了一些文章。但问题是它不调用它就画泰国线。我想通过点击按钮来画线..而不是在主窗体上,而是在面板上..我如何修改这段代码这是我的代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package graphic;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
*
* @author nisansala
*/
public class grapix extends javax.swing.JFrame {
/**
* Creates new form grapix
*/
public grapix() {
initComponents();
}
@Override
public void paint(Graphics g) {
Graphics2D ga = (Graphics2D)g;
ga.setPaint(Color.red);
ga.drawLine(200,100,200,300);
}
/**
* 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() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("paint");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(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()
.addGap(153, 153, 153)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(153, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(108, 108, 108)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(149, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Graphics gg = null;
// TODO add your handling code here:
// grap(gg);
}
/**
* @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(grapix.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(grapix.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(grapix.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(grapix.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 grapix().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
3条答案
按热度按时间zc0qhyus1#
问题:按钮事件处理程序中没有事件代码。
尝试在构造函数之前创建一个名为boolean showline的状态变量。
在构造函数中将其初始化为false。
单击按钮时修改状态变量。
}
将条件检查添加到绘制方法。
@重写公共void paint(graphics g){graphics2d ga=(graphics2d)g;
}
q1qsirdb2#
下面是一个帮助您的示例:
1) 我不建议通过netbeans拖放来学习swing,首先用手来做,而不是使用拖放来让你的生活更轻松,一旦你了解了事情是如何工作的。
2) 不要伸出手来
JFrame
(对于面向对象的最佳实践)我们不扩展类,除非我们向它们添加它们不支持的功能3) 不重写
paint
的JFrame
除非绝对必要JComponent
就像JPanel
和覆盖paintComponent
pb3skfrl3#
请添加要从按钮侦听器调用的正确方法。同时添加要绘制线的面板。这似乎是一个可能的重复:-
在框架上画一条线