JFrameフォームにラベルとボタンがあります。私がしたいことは、ユーザーがボタンをクリックすると、ラベルが黒色になります。 JLabelはコードを記述しても不透明になりませんか?
は私が右、黒のラベルと設定した背景をクリックした後、私はJButtonのダブルクリックと私はlbl.setOpaque(true);
を書いた
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
方法では、フォーム上に行ってきましたしかし、テストランのボタンをクリックしても、それは黒くならないでしょう!私が気付いたもう一つの点は、setToolTipText()
メソッドを使用して、マウスをラベルから外すと(つまり、ラベルを外す)、その領域は意図された黒色のままであるということでした。
この「不具合」が起こっている理由は誰にもありますか?
EDIT: - ここにはscreenshotが役立ちます。
更新: - あなたのメソッド@coinbirdを試しましたが、それでも動作しません!それはlbl.setOpaque(true);
は何でもするだろう前に、背景色を設定する必要があります。..
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
public class background_test2 extends javax.swing.JFrame {
/**
* Creates new form background_test2
*/
public background_test2() {
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() {
jButton1 = new javax.swing.JButton();
lbl = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
lbl.setBackground(new java.awt.Color(0, 0, 0));
lbl.setForeground(new java.awt.Color(255, 0, 0));
lbl.setText("Hover HERE!!");
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(44, 44, 44)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(151, 151, 151)
.addComponent(lbl, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(94, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(lbl, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(122, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JLabel lbl2 = new JLabel("Test");
lbl2.setBackground(Color.BLACK);
lbl2.setOpaque(true);
}
/**
* @param args the command line arguments
*/
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(background_test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(background_test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(background_test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(background_test2.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 background_test2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel lbl;
// End of variables declaration
}
私はそのGUIツールボックスで設定したにもかかわらず、なぜ私はそれを動作させることができないのですか?私はそれがsetToolTipText()メソッドの後に黒くなることを意味します.. –
もう一度同じ問題を試しました:( –
@TrafalgarLawトラブルシューティングしてみましょう。ボタンをクリックして外しても問題ありませんか? 'JLabel lbl = new JLabelの( "テスト"); lb.setBackground(Color.BLACK); lb.setOpaque(真); ' – coinbird