写入值并单击按钮打开浏览器

Write value and click button open browser

我尝试 Select 来自 JComboBox 的案例,然后写入一个值并打开该网站 "case+value"。但是当我单击按钮时收到此错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.awt.Button cannot be cast to javax.swing.JComboBox
    at Deneme1.actionPerformed(Deneme1.java:52)
    at java.awt.Button.processActionEvent(Button.java:409)
    at java.awt.Button.processEvent(Button.java:377)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access0(EventQueue.java:97)
    at java.awt.EventQueue.run(EventQueue.java:709)
    at java.awt.EventQueue.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue.run(EventQueue.java:731)
    at java.awt.EventQueue.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

有我的源代码:

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.URI;
import javax.swing.*;
import java.net.URISyntaxException;

public class Deneme1 extends Frame implements WindowListener, ActionListener {

    JTextField Value = new JTextField(20);
    Button Try;

    public static void main(String[] args) {
        Deneme1 Windows = new Deneme1("Deneme1 ");
        Windows.setSize(400, 200);
        Windows.setLocation(430, 100);
        Windows.setVisible(true);
        Windows.setTitle("Title");
    }

    public Deneme1(String title) {

        JLabel Text2 = new JLabel("Pick one: ");
        add(Text2);
        JComboBox Select = new JComboBox();
        Select.addItem("A");
        Select.addItem("B");
        add(Select);
        JLabel Text1 = new JLabel("Enter Value:");
        setLayout(new FlowLayout());
        addWindowListener(this);
        Try = new Button("Try");
        add(Text1);
        add(Value);
        add(Try);
        Try.addActionListener(this);
    }

    public void actionPerformed(ActionEvent E) {

        JComboBox<String> Select = (JComboBox<String>) E.getSource();
        String Selected = (String) Select.getSelectedItem();
        switch (Selected) {
            case "A":
                try {
                    URI AcilacakSite = new URI("https://A" + Value.getText());
                    Desktop.getDesktop().browse(AcilacakSite);
                } catch (URISyntaxException | IOException ex) {
                    JOptionPane.showMessageDialog(null, "ERR");
                }
                break;
            case "B":
                try {
                    URI AcilacakSite = new URI("https://B" + Value.getText());
                    Desktop.getDesktop().browse(AcilacakSite);
                } catch (URISyntaxException | IOException ex) {
                    JOptionPane.showMessageDialog(null, "ERR");
                }
                break;
            default:
                JOptionPane.showMessageDialog(null, "ERR");
                break;
        }
    }

    @Override
    public void windowOpened(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To     change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);

    }

    @Override
    public void windowClosed(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To     change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowIconified(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowActivated(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

此代码通过将组合框声明为 class 属性并在执行的操作方法中直接访问它来修复 ClassCastException

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import java.io.IOException;

public class Deneme1 extends JFrame implements ActionListener {

    JTextField value = new JTextField(20);
    JButton tryFetch;
    // declared as a class attribute, this can be accessed 
    // in the action performed method
    JComboBox select; 

    public static void main(String[] args) {
        Deneme1 windows = new Deneme1("Deneme1 ");
        windows.setSize(400, 200);
        windows.setLocation(430, 100);
        windows.setVisible(true);
        windows.setTitle("Title");
    }

    public Deneme1(String title) {
        JLabel text2 = new JLabel("Pick one: ");
        add(text2);
        select = new JComboBox();
        select.addItem("A");
        select.addItem("B");
        add(select);
        JLabel text1 = new JLabel("Enter value:");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tryFetch = new JButton("Try");
        add(text1);
        add(value);
        add(tryFetch);
        tryFetch.addActionListener(this);
    }

    public void actionPerformed(ActionEvent E) {
        String selected = (String) select.getSelectedItem();
        switch (selected) {
            case "A":
                try {
                    URI AcilacakSite = new URI("https://A" + value.getText());
                    Desktop.getDesktop().browse(AcilacakSite);
                } catch (URISyntaxException | IOException ex) {
                    JOptionPane.showMessageDialog(null, "ERR");
                }
                break;
            case "B":
                try {
                    URI AcilacakSite = new URI("https://B" + value.getText());
                    Desktop.getDesktop().browse(AcilacakSite);
                } catch (URISyntaxException | IOException ex) {
                    JOptionPane.showMessageDialog(null, "ERR");
                }
                break;
            default:
                JOptionPane.showMessageDialog(null, "ERR");
                break;
        }
    }
}

注意:我修复了问题评论中提到的大部分内容。这段代码还有问题。