我想在点击其中一个按钮时添加弹出窗口
以下是代码:
这是子类
public class Pajero extends Mobil implements ActionMobil{
@Override
public void kecepatan() {
System.out.println("Mobil ngegas");
}
@Override
public void rem() {
System.out.println("Mobil ngerem");
}
@Override
public void belokKiri() {
System.out.println("Mobil belok kiri");
}
@Override
public void belokKanan() {
System.out.println("Mobil belok kanan");
}
@Override
public void percepatMobil() {
System.out.println("Mobil sekarang berjalan lebih cepat");
}
@Override
public void perlambatMobil() {
System.out.println("Mobil sekarang berjalan lebih lambat");
}
}
这是主要的课程
public class Main {
private final ActionMobil setir;
public Main(ActionMobil Setir) {
this.setir = Setir;
}
public void TampilTipeMobil(){
setir.tampilTipe();
}
void drive() {
setir.kecepatan();
setir.rem();
setir.belokKiri();
setir.belokKanan();
}
void percepat(){
setir.percepatMobil();
}
void perlambat(){
setir.perlambatMobil();
}
public static void main(String[] args) {
}
这是框架类
package KendaraanDua;
/**
*
* @author Marsu
*/
public class Frame extends javax.swing.JFrame {
/**
* Creates new form Frame
*/
public Frame() {
initComponents();
}
private Frame(Pajero pajero) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jOptionPane1 = new javax.swing.JOptionPane();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
BtnPercepat = new javax.swing.JButton();
BtnPerlambat = new javax.swing.JButton();
PopUp = new javax.swing.JOptionPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Kontrol Mobil");
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel1.setText("Kontrol Pada Mobil");
jLabel2.setText("- Mobil ngegas");
jLabel3.setText("- Mobil ngerem");
jLabel4.setText("- Mobil belok kiri");
jLabel5.setText("- Mobil belok kanan");
BtnPercepat.setText("Percepat");
BtnPercepat.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnPercepatActionPerformed(evt);
}
});
BtnPerlambat.setText("Perlambat");
BtnPerlambat.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnPerlambatActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(141, 141, 141)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(96, 96, 96)
.addComponent(BtnPercepat)
.addGap(45, 45, 45)
.addComponent(BtnPerlambat))
.addGroup(layout.createSequentialGroup()
.addGap(118, 118, 118)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel4)
.addComponent(jLabel5))))
.addContainerGap(103, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(PopUp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(62, 62, 62))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(BtnPerlambat)
.addComponent(BtnPercepat))
.addGap(9, 9, 9)
.addComponent(PopUp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void BtnPercepatActionPerformed(java.awt.event.ActionEvent evt) {
/** Shows ControlMobil.percepat(); on optionpane when its clicked **/
}
private void BtnPerlambatActionPerformed(java.awt.event.ActionEvent evt) {
/** Shows ControlMobil.perlambat(); on optionpane when its clicked **/
}
private void drive() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private void percepat() {
}
public static void main(String args[]) {
System.out.println("---Penggunaan pengkontrolan pada Mobil---");
System.out.println();
System.out.println("---Kontrol pada Mobil---");
Main ControlMobil;
ControlMobil = new Main(new Pajero());
ControlMobil.drive();
System.out.println();
System.out.println("---Action---");
ControlMobil.percepat();
ControlMobil.perlambat();
System.out.println("");
}
// Variables declaration - do not modify
private javax.swing.JButton BtnPercepat;
private javax.swing.JButton BtnPerlambat;
private javax.swing.JOptionPane PopUp;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JOptionPane jOptionPane1;
// End of variables declaration
}
mobil class
package Kendaraan;
public abstract class Mobil{
public String merkMobil;
public void tampilTipe(){
System.out.println("Pajero");
}
}
actionMobil类
package Kendaraan;
public interface ActionMobil {
public void kecepatan();
public void rem();
public void belokKiri();
public void belokKanan();
public void percepatMobil();
public void perlambatMobil();
public void tampilTipe();
}
正如您所看到的,我想在ControlMobil.percepat();
上显示BtnPercepat
并将其显示在选项窗格上。
答案 0 :(得分:0)
对于我认为你想要实现的目标,你需要改变一些事情。 显示的选项窗格出现在您注释掉的方法中,假设该对象是一个类字段,在Frame.java中:
Main controlMobil2 = new Main(new Pajero());
private void BtnPercepatActionPerformed(java.awt.event.ActionEvent evt) {
/** Shows ControlMobil.percepat(); on optionpane when its clicked **/
JOptionPane.showMessageDialog(this, controlMobil2.percepat());
}
这会强制main上的percepat返回一个字符串Main.java:
String percepat(){
return setir.percepatMobil();
}
反过来强制接口返回要打印的String,而不是将其写入控制台:
public interface ActionMobil {
...
public String percepatMobil();
...
}
以及pajero类:
public class Pajero extends Mobil implements ActionMobil{
...
@Override
public String percepatMobil() {
//System.out.println("Mobil sekarang berjalan lebih cepat");
return "Mobil sekarang berjalan lebih cepat";
}
...
}
作为旁注,命名约定规定变量应以小写字母开头。