标签: jbutton

为什么按钮不反映Action'NAME属性?

我想按钮应该在下面的代码中有"关闭"标题,但它没有:

public class Test_Actions extends JDialog 
{   
    private AbstractAction closeAction = new AbstractAction() 
    {       
        {
            putValue("NAME", "Close");
        }

        @Override
        public void actionPerformed(ActionEvent arg0) 
        {
            Test_Actions.this.setVisible(false);
            Test_Actions.this.dispatchEvent(new WindowEvent(Test_Actions.this, WindowEvent.WINDOW_CLOSING));
        }       
    };

    public Test_Actions() 
    {       
        JLabel label = new JLabel("Hello world");

        JButton button = new JButton(closeAction);
        //button.setText("Text");

        setLayout(new BorderLayout());
        add(label, BorderLayout.CENTER);
        add(button, BorderLayout.SOUTH);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }

    public static void main(String[] args) throws InterruptedException, InvocationTargetException 
    {
        final Test_Actions dialog = new Test_Actions();
        dialog.setModal(true);

        SwingUtilities.invokeAndWait(new Runnable() 
        {
            @Override
            public void run() 
            {
                dialog.pack();
                dialog.setVisible(true);
            } …
Run Code Online (Sandbox Code Playgroud)

java swing action modal-dialog jbutton

2
推荐指数
1
解决办法
219
查看次数

JBoolton中的抽象动作在没有文本的JToolbar中

我的java swing应用程序有一些在JMenuItems和JButtons中使用的AbstractActions.我想把它们中的一些放在JButton里面的JToolbar中,但我只想要显示图标,而不是文本.有最佳实践方法吗?

java icons swing jbutton jtoolbar

2
推荐指数
1
解决办法
3964
查看次数

从另一个JButton获取JButton

当我点击另一个jbutton时,我想得到一个jbutton.

这里是示例代码的链接(以jbutton身份登录,asdf作为密码)

//File Name= test1.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class test1 extends JFrame {
    public static void main(String[] args) {
        new test1();
    }

    public test1() {
        super("Using JButton");
        Container content = getContentPane();
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout());
        JButton button = new JButton("First");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("You clicked first button");
            }
        });
        content.add(button);
        JButton button2 = new JButton("Second");
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("You clicked second button");
            }
        });
        content.add(button2);
        pack();
        setVisible(true); …
Run Code Online (Sandbox Code Playgroud)

java swing jbutton

2
推荐指数
1
解决办法
922
查看次数

从JComboBox获取价值

我有JComboBox2列,我有JButton.单击时JButton,我需要分别JComboBox从第一列和第二列中获取所选值的结果...

我怎么这样?

另外:如何设置JComboBox的标头?

代码:

 public class Combo extends JFrame implements ActionListener{
    private JComboBox combo = new JComboBox();
    private JButton button = new JButton();
    public Combo() {

        setLayout(new FlowLayout());
        combo.setRenderer(new render());

        add(combo);

        combo.addItem(new String[] {"1","bbb"});
        combo.addItem(new String[] {"2","ff"});
        combo.addItem(new String[] {"3","gg"});
        combo.addItem(new String[] {"4","ee"});

        add(button);
        button.addActionListener(this);
        pack();
    }


    public static void main(String[]args){
        new Combo().setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==button){
            System.out.println(combo.getSelectedItem());
        }
    }
}
class render extends JPanel implements ListCellRenderer{

    private …
Run Code Online (Sandbox Code Playgroud)

java swing jbutton jcombobox listcellrenderer

2
推荐指数
1
解决办法
5万
查看次数

单击获取JButton的名称

  @Override
  public void actionPerformed(ActionEvent e) {
      if (e.getSource() == thirdBtn) {
          //System.out.println("Third Button Click");
          System.out.println(e.getSource()+" Click");
      }
  }
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我想知道是否而不是这样做:

//System.out.println("Third Button Click");
Run Code Online (Sandbox Code Playgroud)

如果我能做这样的事情:

System.out.println(e.getSource()+" Click");
Run Code Online (Sandbox Code Playgroud)

但是代码输出:

BlackJack.OverBoard$BlackJackButton[,440,395,100x25,alignmentX=0.0,alignmentY=0.5,
    border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@7a3d8738,
    flags=16777504,maximumSize=,minimumSize=,preferredSize=,
    defaultIcon=,disabledIcon=,disabledSelectedIcon=,
    margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],
    paintBorder=false,paintFocus=true,
    pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,
    text=Change,defaultCapable=true] Click
Run Code Online (Sandbox Code Playgroud)

我不想要这个,我想知道如何获取JButton名称并在点击时输出它.

编辑:

有些人很困惑.当我说"名字"(也许这是错误的词)时,我的意思是说你初始化了一个JButton

JButton btnExample = new JButton();
Run Code Online (Sandbox Code Playgroud)

我想要它,以便当您单击按钮时,它btnExample在控制台中输出.

java swing jbutton output actionevent

2
推荐指数
2
解决办法
2万
查看次数

自定义按钮不能在mac上工作(ButtonUI)

我有一个应用程序,它使用遍布文本和图标的自定义按钮.适用于Windows和Linux,但现在OSX用户抱怨.文本不会显示在Mac上,只是'...'.代码看起来很简单,但是当涉及到mac时我很无能为力.我怎样才能解决这个问题?

测试用例:

package example.swingx;
import example.utils.ResourceLoader;
import com.xduke.xlayouts.XTableLayout;    
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import java.awt.*;
import java.awt.event.ActionEvent;

public class SmallButton extends JButton {
    private static ComponentUI ui = new SmallButtonUI();
    public SmallButton() {
        super();
        /*  final RepaintManager repaintManager = RepaintManager.currentManager(this);
        repaintManager.setDoubleBufferingEnabled(false);
        setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);*/
    }
    public SmallButton(AbstractAction action) {
        super(action);
    }
    public SmallButton(String text) {
        super(text);
    }
    public SmallButton(Icon icon) {
        super(icon);
    }
    public void updateUI() {
        setUI(ui);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel …
Run Code Online (Sandbox Code Playgroud)

java macos swing look-and-feel jbutton

2
推荐指数
1
解决办法
3606
查看次数

如何在JComponent中绘制JComponent?

我对JComponent有一个奇怪的问题。我正在尝试创建自己的JComponent,因此需要将JComponents组合在一起。

我想在我的JComponent JDial中绘制JButton:

public class JDial extends JComponent {
    private static final long serialVersionUID = 3364481508702147328L;

    public JDial() {        
        JButton b = new JButton("test");
        this.add(b);
    }
}   
Run Code Online (Sandbox Code Playgroud)

但这只是一无所有。更加有趣的是,这一方法效果很好:

public class JDial extends JPanel {
    private static final long serialVersionUID = 3364481508702147328L;

    public JDial() {        
        JButton b = new JButton("test");
        this.add(b);
    }
}
Run Code Online (Sandbox Code Playgroud)

JPanel继承自JComponent并在其中绘制JButton。JPanel如何做到这一点?

提前致谢

java swing jcomponent jpanel jbutton

2
推荐指数
1
解决办法
904
查看次数

单击按钮后GUI冻结

我正在尝试创建一个客户端/服务器应用程序.问题是客户端的确认按钮第一次输入工作,但如果我为另一个输入点击相同的按钮,客户端GUI将冻结.

为什么GUI会冻结?

客户:

public class ClientGui 
{

private JFrame frmBookPointOf;
private JTextField txtRm;
private JTextArea txtrBookDetails;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JComboBox comboBox_1;
private JComboBox comboBox;
private dbController dbCtrl;
private Connection conn;
private static Socket client; 
private static BufferedReader fromServer = null;
private static DataOutputStream toServer = null;

/**
 * Launch the application.
 */
public static void main(String[] args) {

    try {


        client = new Socket("127.0.0.1", …
Run Code Online (Sandbox Code Playgroud)

java swing multithreading jbutton event-dispatch-thread

2
推荐指数
1
解决办法
5528
查看次数

如何删除GUI中的选择框?

在任何程序中,或者至少大多数,当你选择一个按钮或任何东西时,有一个由点组成的选择框.

你怎么摆脱那个盒子?

我想要这样做的原因是因为我有一个带图像的按钮,没有contentFill,没有边框,并且在选择时看起来很尴尬.

java swing selection jbutton

2
推荐指数
1
解决办法
101
查看次数

如果按下按钮,则重新启动我的GUI

我有一个全局变量框架,即JFrame.

public static void setUp(final Wheel []player, final phraser p) throws IOException {        
    final JPanel scorePanel=new JPanel();
    final JPanel namePanel=new JPanel();
    panel=new JPanel(new GridLayout(1,player.length,1,1));
    panel1=new JPanel();
    panel2=new JPanel();
    panel3=new JPanel(new GridLayout(2,1,1,1));

    panel3.add(new JLabel("Dead letters/phrases:"));
    panel3.add(LettersOrPhGuessed);
    JMenuBar menuBar=new JMenuBar();
    frame.setJMenuBar(menuBar);
    clock=new JMenu();
    JMenu file=new JMenu("File");       
    JMenuItem exit=new JMenuItem("Exit");
    JMenuItem reset=new JMenuItem("Reset");
    file.add(exit);
    file.add(reset);
    menuBar.add(file);
    menuBar.add(clock);

    exit.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }
    });

    reset.addActionListener(new ActionListener(){//help me
        public void actionPerformed(ActionEvent e){
            frame.removeAll();
            frame.validate();
            frame.setVisible(false);
            try {
                startApp();
            } catch (IOException e1) …
Run Code Online (Sandbox Code Playgroud)

java swing jpanel jframe jbutton

2
推荐指数
1
解决办法
2万
查看次数