标签: jbutton

启用/禁用JButton

我正在开发一个小型照片编辑应用程序,并希望JButton禁用,直到用户加载图像,此时我希望按钮启用(可点击).我的想法是添加一个布尔imageFound和一个图像检查器方法.如果boolean为false,则禁用按钮,如果为true,则启用它们(在load image actionPerformed方法中boolean设置为true).我遇到的问题是,当运行应用程序时,按钮会被禁用,但是当我加载图像时,它们仍然会被禁用.我不知道是否可能缺少任何代码来重新检查图像是否可用,从而启用按钮(在运行时).谢谢你的帮助.

...BufferedImage effects = null;
boolean bmpFound = false;


public GUI()
{        
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    this.initComponents();
    this.bmpChecker();
    this.addListeners();        
    this.setTitle("PicTweak");
    this.setSize(900, 600);
    this.setVisible(true);
}

...else if(e.getSource() == loadItem)
    {            
        try
        {
            imagePath = DialogIO.displayOpenDialog();
            effects = ImageInOut.loadImage(imagePath);                
            imageHolder.setIcon(new ImageIcon(effects));
            bmpFound = true;
        }

....public void bmpChecker()
{
    if(bmpFound)
    {
        grayScale.setEnabled(true);
        blur.setEnabled(true);
        reset.setEnabled(true);
        brightDark.setEnabled(true);
        horFlip.setEnabled(true);
        verFlip.setEnabled(true);
        verHorFlip.setEnabled(true);
    }
    else 
    {
        grayScale.setEnabled(false);
        blur.setEnabled(false);
        reset.setEnabled(false);
        brightDark.setEnabled(false);
        horFlip.setEnabled(false);
        verFlip.setEnabled(false);
        verHorFlip.setEnabled(false);
    }
}
Run Code Online (Sandbox Code Playgroud)

java swing jbutton

0
推荐指数
1
解决办法
2371
查看次数

自定义渐变按钮 - 无法看到文本

我正在制作一个带有渐变效果的自定义按钮.我能够设置渐变效果,但我看不到文字.我哪里错了?

class CustomButton extends JButton {   
    Color color1, color2;   

    public CustomButton(String text, Color color1, Color color2) {   
        super(text);   
        this.color1 = color1;   
        this.color2 = color2;   
        setOpaque(false);   
        setSize(new Dimension(450, 350));
        setForeground(Color.white);
        setText(text);
        setContentAreaFilled(false);
    }   

    protected void paintComponent(Graphics g) {    
        super.paintComponent(g);
        int width = getWidth();   
        int height = getHeight();   

        GradientPaint paint = new GradientPaint(0, 0, color1, width, height,
                color2, true);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);   
        Paint oldPaint = g2d.getPaint();
        g2d.setPaint(paint);
        g2d.fillRect(0, 0, width, height);
        g2d.drawString("Button 1", getWidth()/2, 10);
        g2d.setPaint(oldPaint);
    }   
}  
Run Code Online (Sandbox Code Playgroud)

注意:我允许用户在运行时更改颜色.根据更改的颜色,我相应地设置背景.

java swing gradient jbutton

0
推荐指数
1
解决办法
2593
查看次数

重复执行SwingWorker

我正在编写一个可以进行音频分析的应用程序(用作吉他调音器,增加了和弦估计和其他一些东西),但我在使用GUI时遇到了一些问题.第一个问题是我有一个按钮,点击后应更改其显示的文本.单击它时,文本不会更改,但是应该更改应该更改它的代码行.

另一件事是我需要一个SwingWorker来重复执行(一旦完成就重新启动)并每次更新GUI.正如我的代码目前,我有一个while循环重复执行我的SwingWorker,但这导致GUI变得无响应,因为它在EDT中运行(大概).让SwingWorker重复执行的最佳方法是什么?我应该创建一个新线程来运行循环,还是其他什么?

我内部ActionListener类的代码如下:

private class TunerListener implements ActionListener {
    private boolean firstUpdate = true;
    private volatile boolean executing = false;

    private final SwingWorker<Void, Void> tunerWorker = new SwingWorker<Void, Void>() {

        @Override
        protected Void doInBackground() {
            model.update();
            return null;
        }

        @Override
        protected void done() {
            if (!this.isCancelled()) {
                prev.setText(model.getPrev());
                currentNote.setText(model.getNote());
                next.setText(model.getNext());
                frequency.setText(model.getFrequency());
                switch (model.getOffset()) {
                    case -2:
                        light_2.setIcon(onRed);
                        light_1.setIcon(off);
                        light0.setIcon(offBig);
                        light1.setIcon(off);
                        light2.setIcon(off);
                        break;
                    case -1:
                        light_2.setIcon(off);
                        light_1.setIcon(onRed);
                        light0.setIcon(offBig);
                        light1.setIcon(off);
                        light2.setIcon(off);
                        break;
                    case 0:
                        light_2.setIcon(off);
                        light_1.setIcon(off);
                        light0.setIcon(onGreen);
                        light1.setIcon(off);
                        light2.setIcon(off);
                        break;
                    case 1: …
Run Code Online (Sandbox Code Playgroud)

java swing execute swingworker jbutton

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

button3_Click(sender,e)的等价物是什么; 在Java?

button3_Click(sender, e);用Java 调用相当于什么?

我正在尝试进行文本字段操作(点击Enter)触发按钮的代码.

谢谢!

java swing action call jbutton

0
推荐指数
1
解决办法
213
查看次数

使JButton将文本从JTextField插入变量?

我正在尝试编写一个程序,它将在JTextField中获取文本并将其放入我按下JButton时声明的变量中.我需要计算一个学校项目的每周工资,但这只需要控制台,我正在为自己的乐趣做GUI.我试图得到它所以当我点击'calc'时它将从id,rh,oh,hp等取得输入并计算每周支付(wp),然后将其打印在旁边的右侧列上计算按钮.

//the calculations aren't complete yet until I finish the GUI

public class Weekly_Pay 
{

public static void calculations(String[] args) 
{

Scanner imput = new Scanner(System.in);

System.out.println("ID number: ");
int employeeId = imput.nextInt();

System.out.println("Hourly Wage: ");
Double hourlyWage = imput.nextDouble();

System.out.println("Regular Hours: ");
double regularHours = imput.nextDouble();

System.out.println("Overtime Hours: ");
double overtimeHours = imput.nextDouble();

double overtimePay = round(overtimeHours * (1.5 * hourlyWage));
double regularPay  = round(hourlyWage * regularHours);

double weeklyPay = regularPay + overtimePay;

System.out.println("Employee ID Number:" + employeeId);
System.out.printf("Weekly …
Run Code Online (Sandbox Code Playgroud)

java variables swing jbutton jtextfield

0
推荐指数
1
解决办法
6949
查看次数

每个循环java中的数组错误

我正在使用swing UI进行基本的刽子手游戏的代码.我正在使用for循环来启动字母的所有按钮.但是我在第39行得到一个空指针异常.我已经查看了它并且不确定为什么它不能正常工作.底部10行左右的代码是被抛出的问题.

    import java.awt.Color;
    import javax.swing.*;

    public class GameUI {

    public static void main(String[] args){
        GameUI ui = new GameUI();
        ui.initializeUI();
    }

    public void initializeUI(){
        //initialize the window
        JFrame window = new JFrame();
        window.setSize(500,500);
        window.setResizable(false);
        window.setVisible(true);

        //initialize main panel
        JPanel wrapper = new JPanel();
        wrapper.setLayout(null);
        Color BGColor = new Color(240,230,140);
        wrapper.setBackground(BGColor);
        window.getContentPane().add(wrapper);

        //Creates JLAbel title, this is used for the title of the game
        JLabel title = new JLabel("Hangman v0.1");
        title.setBounds(10, 5, 200, 50);
        wrapper.add(title);
        //=================================================================
        //Creates JButtons for each …
Run Code Online (Sandbox Code Playgroud)

java arrays swing jbutton

0
推荐指数
1
解决办法
191
查看次数

从actionlistener调用swing JPanels

即时通讯使用各种类编码项目,但当我按下一个调用我的主菜单的按钮时,我卡住了,这是我恢复的代码:

我的主要课程:

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        Dimension res = Toolkit.getDefaultToolkit().getScreenSize();

public void run(){

    Principal frame = new Principal("Program");

    frame.setSize(res.width,res.height);

    frame.setResizable(false);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setUndecorated(true);

    frame.setVisible(true); 
    }
});`
Run Code Online (Sandbox Code Playgroud)

现在,我的校长:

 public class Principal extends JFrame implements ActionListener {


     private DetallesPrincipal DetallesPrincipal;

     private ClientesNuevo ClientesNuevo;

     private ClientesModificar ClientesModificar;

     Container p;

     private static final long serialVersionUID = 1L;

     final JMenuItem Clientes1,Clientes2,Clientes3;

     final JMenuBar MenuBarMain = new JMenuBar();

 public Principal (String titulo){

      super(titulo); 

          setLayout(new BorderLayout());

      final JMenuBar MenuBarMain = new JMenuBar();

        MenuBarMain.setBackground(new …
Run Code Online (Sandbox Code Playgroud)

java swing jpanel jbutton actionlistener

0
推荐指数
1
解决办法
1418
查看次数

JButton + AbstractAction重置了一堆JCheckBox(s) - Swing

我正在ToolBar用一个JButton和一个JCheckBoxs 创建一个隐藏或显示列的列JTable.

JButton主要目的是,一旦点击重置所有其他JCheckBoxToolBar要么全部选中或全部选中.实际上我假装一旦点击重置就检查所有这些.

我可以创建它们并放置它们,这里没有问题.

我的问题是如何让JButton一旦点击就重置了所有的JCheckBox ToolBar.

这是我的代码,我在其中创建并添加Action.

        final JToolBar toolBarTop = new JToolBar();

        // The Reset Button
        toolBarTop.add(new JButton(new AbstractAction("Reset") {
            @Override
            public void actionPerformed(ActionEvent e) {
            columnModel.setAllColumnsVisible();
            }   
        }));

        // Create a JCheckBox for each column
        for(int i = 0; i < labelsCheckBox.size(); i++)
        {
            final int index = i;
            toolBarTop.add(new JCheckBox(new AbstractAction(labelsCheckBox.get(i)) {
            @Override
                public void actionPerformed(ActionEvent e) {
                TableColumn column …
Run Code Online (Sandbox Code Playgroud)

java swing jbutton jcheckbox

0
推荐指数
1
解决办法
788
查看次数

如何用循环删除Java JButtons?

我正在用java构建一个程序,我想知道是否有任何函数可以在按下时删除一个JButton列表?

这是我到目前为止:

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == button[0]) {
        for(int x = 0; x < 19; x++) {
            button[x].remove(this);

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ActionListener已经配置,它工作正常.提前感谢谁给了我一个解决方案.

java swing jbutton actionlistener

0
推荐指数
1
解决办法
110
查看次数

将值添加到JButton的位置

如果我使用Swing创建了一个JButton(名为butOne),并希望按钮移动说,单击时向右移动10个像素,我是否有其他选择:

butOne.setLocation(butOne.getX() + 10, butOne.getY())

butOne.left -= 10或许类似于Visual Basic的东西?将X坐标和Y坐标仅仅改变一个似乎是一个非常不方便的解决方法.谢谢.

java swing jbutton

0
推荐指数
1
解决办法
38
查看次数