Netbeans中GUI组件的自定义Bean类 [英] Custom Bean Class for GUI Component in Netbeans
问题描述
好的,所以我的教授发布了一个作业,但是我们应该使用的参考代码使我感到困惑.他制作了一个JFrame,并在该Jframe中放入了一个按钮,但是该按钮的bean类是JHoverButton.Java的子类,该子类扩展了JButton.当我尝试在代码中执行此操作时,无法使Bean类成为我自己的子类.我尝试了自定义创建代码,尝试了绑定,并且在堆栈溢出时四处张望,但我找不到真正的答案.任何帮助将不胜感激!
Alright so my professor posted up an assignment but the reference code that we're supposed to use is confusing me. He made a JFrame and inside that Jframe he put in a button, but the beanclass for that button is the subclass JHoverButton.Java which extends JButton. When I try to do that in my code, I can't get the Bean Class to be my own sub class. I've tried custom creation code, I've tried binding and I've looked around on stack overflow but I couldn't really find an answer. Any help would be greatly appreciated!
这是我的老师发布的
这就是我要坚持的地方.
This is where I'm stuck.
我们将不胜感激,谢谢!
Any help would really be appreciated, thank you!
BeanProjectTest.Java的源代码:
Source Code for BeanProjectTest.Java:
package beanproject;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.metal.*;
public class BeanProjectTest extends javax.swing.JFrame {
/**
* Creates new form BeanProjectTest
*/
public BeanProjectTest() {
initComponents();
try{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){
JOptionPane.showMessageDialog(this, e.toString());
}
}
/**
* 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() {
jColorChooser1 = new javax.swing.JColorChooser();
jHoverButton1 = new beanproject.JHoverButton();
jIntegerField1 = new beanproject.JIntegerField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Bean Project Test");
jHoverButton1.setText("jHoverButton1");
jIntegerField1.setText("jIntegerField1");
jButton1.setText("jButton1");
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(38, 38, 38)
.addComponent(jHoverButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(53, 53, 53))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jIntegerField1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jHoverButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(72, 72, 72)
.addComponent(jIntegerField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(241, Short.MAX_VALUE))
);
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
*/
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BeanProjectTest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JColorChooser jColorChooser1;
private beanproject.JHoverButton jHoverButton1;
private beanproject.JIntegerField jIntegerField1;
// End of variables declaration
}
JHoverButton.Java的源代码:
Source Code For JHoverButton.Java:
public class JHoverButton extends JButton implements MouseListener{
public JHoverButton(){
super();
initialize();
}
private void initialize(){
setBorderPainted(false);
addMouseListener(this);
}
public JHoverButton(String text){
super(text);
initialize();
}
public JHoverButton(String text, Icon icon){
super(text, icon);
initialize();
}
public void setEnabled(boolean enabled){
super.setEnabled(enabled);
if(enabled){
if(isBorderPainted()){
setBorderPainted(false);
repaint();
}
}
}
@Override
public void mouseClicked(MouseEvent me) {
}
@Override
public void mousePressed(MouseEvent me) {
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
if(!isBorderPainted() && isEnabled()){
setBorderPainted(true);
repaint();
}
}
@Override
public void mouseExited(MouseEvent me) {
if(isBorderPainted()){
setBorderPainted(false);
repaint();
}
}
}
推荐答案
首先,请注意,如果您手动编写GUI而不是使用GUI构建器,则添加第三方组件会更加容易:
As a starter please note that adding a third-party component would be really easier if you code your GUI by hand instead of using a GUI builder:
JPanel panel = new JPanel();
panel.add(new JHoverButton("Hello!"));
...
frame.add(panel);
现在已经说过了,而不是从调色板中添加JButton,您需要添加一个新的Bean并指定组件的完整路径:packagename.ComponentName
.
Now having said that, instead of adding a JButton from the palette you need to add a new Bean and specify the full path to your component: packagename.ComponentName
.
这篇关于Netbeans中GUI组件的自定义Bean类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!