この問題で私を助けることができる人はいますか? Java アプリケーションを Netbeans で直接実行すると、100% 正常に動作します。LEDを制御できます。しかし、プロジェクトをビルドしてビルドした jar ファイルを実行すると、アプリケーションは表示されますが、ボタンをクリックするたびに LED を制御できないようです。
ソースコード:
package pi4j_led;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import java.util.logging.Level;
import java.util.logging.Logger;
public class mainWindow extends javax.swing.JFrame {
//create GPIO Controller
final GpioController gpio = GpioFactory.getInstance();
//provision gpio pin #01 as an output pin and turn on
final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "My LED", PinState.HIGH);
public mainWindow() {
initComponents();
pin.setShutdownOptions(true);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
bOn = new javax.swing.JButton();
lbDisplay = new javax.swing.JLabel();
bBlinking = new javax.swing.JButton();
bClose = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Controlling GPIO Pins Using Pi4J Library");
jLabel2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Turn LED OFF and ON");
bOn.setText("ON");
bOn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bOnActionPerformed(evt);
}
});
lbDisplay.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lbDisplay.setText("The LED is turned ON");
bBlinking.setText("BLINKING MODE: OFF");
bBlinking.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bBlinkingActionPerformed(evt);
}
});
bClose.setText("Close");
bClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bCloseActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(bClose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lbDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(bOn, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(bBlinking, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(bOn, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
.addComponent(bBlinking, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(lbDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bClose, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
.addContainerGap())
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void bOnActionPerformed(java.awt.event.ActionEvent evt) {
if (bOn.getText().equals("ON")) {
pin.low();
bOn.setText("OFF");
lbDisplay.setText("The LED is turned OFF");
} else {
pin.high();
bOn.setText("ON");
lbDisplay.setText("The LED is turned ON");
}
}
private void bBlinkingActionPerformed(java.awt.event.ActionEvent evt) {
if (bBlinking.getText().contains("OFF")) {
bBlinking.setText("BLINKING MODE: ON");
bOn.setEnabled(false);
new Thread(() -> {
for (;;) {
pin.low();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(mainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
pin.high();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(mainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
if(bBlinking.getText().contains("OFF")){
break;
}
}
}).start();
}else{
bBlinking.setText("BLINKING MODE: OFF");
bOn.setText("ON");
bOn.setEnabled(true);
lbDisplay.setText("The LED is turned ON");
}
}
private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {
gpio.shutdown();
this.dispose();
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new mainWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bBlinking;
private javax.swing.JButton bClose;
private javax.swing.JButton bOn;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel lbDisplay;
// End of variables declaration
}