对于学校项目,我必须创建一个基本计算器,其功能包括加法,除法等。此计算器还必须包含一个打开网站的按钮。打开网站的计算器和按钮必须位于同一个计算器上。但是,在不同的班级。对于这个项目,我使用Netbeans,特别是用于设计和源代码的JFrame表单。我不是非常了解Java和Java,我只是围绕打开网站的按钮的方法创建了一个类。
这造成了两个问题: 1.在@SurpressWarnings中("未经检查") 我收到了一个错误:
jButton20.setIcon(new javax.swing.ImageIcon(getClass().getResource("/googleonlineretry.jpg"))); // NOI18N
jButton20.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton20ActionPerformed(evt);
}
jButton20是(点击时)打开网站链接的按钮。为了测试我使用谷歌。在围绕方法的类中,我得到一条灰色(灰色/灰色,无论如何)线,表示该方法未被使用,并且" evt"也没用过。这是我正在谈论的课程:
class Pickone extends RekenMachine {
private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {
Desktop browser = Desktop.getDesktop();
try {
browser.browse(new URI("http://google.com"));
}
catch(IOException err) {
}
catch (URISyntaxException err) {
}
}
行:private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) 通过单击Netbeans设计位中的按钮自动创建。
我的主要问题,或者更确切地说是请求,是要弄清楚如何在这个项目中拥有两个类。我花了整整一夜试图解决这个问题。在线阅读,查看我的书,但无法弄清楚。
计算器如下所示:Image of what it looks like
这是完整的源代码,fwi,RekenMachine =计算器和RekenUitslag =文本字段。
public class RekenMachine extends javax.swing.JFrame {
int eersteNummer, tweedeNummer, resultaat;
String Operator, ingevuld, antwoord;
String calculation;
public RekenMachine() {
initComponents();
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
RekenUitslag = new javax.swing.JTextField();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jButton18 = new javax.swing.JButton();
jButton19 = new javax.swing.JButton();
jButton20 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 20, Short.MAX_VALUE)
);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 30, Short.MAX_VALUE)
);
RekenUitslag.setEditable(false);
RekenUitslag.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
RekenUitslag.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
RekenUitslag.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RekenUitslagActionPerformed(evt);
}
});
jButton4.setText("7");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("8");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setText("9");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setText("/");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setText("6");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setText("*");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setText("4");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
jButton11.setText("5");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
jButton12.setText("3");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});
jButton13.setText("+");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});
jButton14.setText("1");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});
jButton15.setText("2");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton15ActionPerformed(evt);
}
});
jButton16.setText("=");
jButton16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton16ActionPerformed(evt);
}
});
jButton17.setText("-");
jButton17.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton17ActionPerformed(evt);
}
});
jButton18.setText("0");
jButton18.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton18ActionPerformed(evt);
}
});
jButton19.setText("C");
jButton19.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton19ActionPerformed(evt);
}
});
jButton20.setIcon(new javax.swing.ImageIcon(getClass().getResource("/googleonlineretry.jpg"))); // NOI18N
jButton20.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton20ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton20, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton18, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton19, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(RekenUitslag)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(34, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(RekenUitslag, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton18, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton19, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jButton20, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void RekenUitslagActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
String ingevuld = RekenUitslag.getText() + jButton14.getText ();
RekenUitslag.setText(ingevuld);
}
private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
String ingevuld = RekenUitslag.getText() + jButton18.getText();
RekenUitslag.setText(ingevuld);
}
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
eersteNummer = Integer.parseInt(RekenUitslag.getText());
RekenUitslag.setText("");
Operator = "+";
}
private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
String ingevuld = RekenUitslag.getText() + jButton15.getText();
RekenUitslag.setText(ingevuld);
}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
String ingevuld = RekenUitslag.getText() + jButton12.getText();
RekenUitslag.setText(ingevuld);
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
String ingevuld = RekenUitslag.getText() + jButton8.getText();
RekenUitslag.setText(ingevuld);
}
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
String ingevuld = RekenUitslag.getText() + jButton11.getText();
RekenUitslag.setText(ingevuld);
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
RekenUitslag.setText(RekenUitslag.getText()+&#34; 4&#34;);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
String ingevuld = RekenUitslag.getText() + jButton4.getText();
RekenUitslag.setText(ingevuld);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
String ingevuld = RekenUitslag.getText() + "8";
RekenUitslag.setText(ingevuld);
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
String ingevuld = RekenUitslag.getText() + jButton6.getText();
RekenUitslag.setText(ingevuld);
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
eersteNummer = Integer.parseInt(RekenUitslag.getText());
RekenUitslag.setText("");
Operator = "/";
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
Operator = "*";
eersteNummer = Integer.parseInt(RekenUitslag.getText());
RekenUitslag.setText( "");
}
private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
Operator = "-";
eersteNummer = Integer.parseInt(RekenUitslag.getText());
RekenUitslag.setText("" ) ;
}
private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {
String ingevuld = RekenUitslag.getText() + jButton19.getText();
RekenUitslag.setText("");
}
private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
/*
String enterstring1 = RekenUitslag.getText();
int eersteNummer = Integer.parseInt(enterstring1);
String enterstring2 = RekenUitslag.getText();
int tweedeNummer = Integer.parseInt(enterstring2);
int resultaat = eersteNummer + tweedeNummer;
RekenUitslag.setText("" + resultaat);
RekenUitslag.getText() ;
RekenUitslag.setText(RekenUitslag.getText() + "=");
*/
String antwoord;
tweedeNummer = Integer.parseInt(RekenUitslag.getText());
RekenUitslag.setText("test");
if (Operator == "+") {
resultaat = eersteNummer + tweedeNummer;
// antwoord = String.format("%.f", resultaat);
RekenUitslag.setText(String.valueOf(resultaat));
}
else if (Operator == "-") {
resultaat = eersteNummer - tweedeNummer;
RekenUitslag.setText(String.valueOf(resultaat));
}
else if (Operator == "*") {
resultaat = eersteNummer * tweedeNummer;
RekenUitslag.setText(String.valueOf(resultaat));
}
else if (Operator == "/") {
resultaat = eersteNummer / tweedeNummer;
RekenUitslag.setText(String.valueOf( resultaat));
}
/* switch(calculation){
case "+":
// resultaat = firstNumer + secondNumer;
// antwoord = String.format("%.0f", resultaat);
// RekenUitslag.setText(antwoord);
break;
case "-":
resultaat = eersteNummer - Double.parseDouble(RekenUitslag.getText());
RekenUitslag.setText(Double.toString(antwoord) + "=");
break;
case "/":
resultaat = eersteNummer / Double.parseDouble(RekenUitslag.getText());
RekenUitslag.setText(Double.toString(resultaat));
break;
case "*":
resultaat = eersteNummer * Double.parseDouble(RekenUitslag.getText());
RekenUitslag.setText(Double.toString(resultaat));
break;
case "=":
resultaat = eersteNummer + Double.parseDouble(RekenUitslag.getText());
RekenUitslag.setText(Double.toString(resultaat));
break;
default:
RekenUitslag.setText("=");
}
*/
}
类Pickone扩展了RekenMachine {
private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {
Desktop browser = Desktop.getDesktop();
try {
browser.browse(new URI("http://google.com"));
}
catch(IOException err) {
}
catch (URISyntaxException err) {
}
}
}
答案 0 :(得分:0)
这是解决问题的方法,但不能保证满足您的需求。
1:制作Pickone
课程以及方法jButton20ActionPerformed
static
static class Pickone extends Test {
public static void jButton20ActionPerformed() {
Desktop browser = Desktop.getDesktop();
try {
browser.browse(new URI("http://google.com"));
} catch (IOException err) {
}
catch (URISyntaxException err) {
}
}
}
2:从方法调用中删除ActionEvent
3:静默调用Pickone
类,如下所示:
jButton20.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Pickone.jButton20ActionPerformed();
}
});
这基本上是以一种非常简单的方式解决您的问题,即使可能不是您需要的(甚至可能不是最好的方法)。随意询问您是否需要澄清。
此外,您应该真正开始使用imports
,这样您就不必每次都写出完整的包名。导入javax.swing.JButton()
变为JButton()
。它们适用于每个类,大多数IDE都可以选择导入所需的所有类