好吧,所以我的教授发布了一项任务,但我们应该使用的参考代码令我感到困惑。他创建了一个JFrame,在Jframe里面放了一个按钮,但是该按钮的beanclass是JHoverButton.Java的子类,它扩展了JButton。当我尝试在我的代码中执行此操作时,我无法将Bean类作为我自己的子类。我已经尝试过自定义创建代码,我尝试过绑定,我已经查看了堆栈溢出但我无法找到答案。任何帮助将不胜感激!
这是我老师发布的
这就是我被困住的地方。
非常感谢任何帮助,谢谢!
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的源代码:
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();
}
}
}
答案 0 :(得分:5)
首先请注意,如果您手动编写GUI而不是使用GUI构建器,那么添加第三方组件会更容易:
JPanel panel = new JPanel();
panel.add(new JHoverButton("Hello!"));
...
frame.add(panel);
现在说,不是从调色板添加JButton,而是需要添加一个新的Bean并指定组件的完整路径:packagename.ComponentName
。
答案 1 :(得分:0)