2017-01-12 26 views
-1

私はしようとしますが、ラベルのためにボタンや設定を無効にできません。 。 私は 1がクリックされた後、私はいくつかのボタンを無効にする :(tiはJButtonの上に追加しますが、いけない仕事しようとしたがbutton.setEnabled(偽);この場合はいけない作業Java Swingを無効にして有効にします

package forms; 

public class FrmAdmEdit extends JFrame { 

private JPanel contentPane; 
private JTextField txbUsuario; 
private JPasswordField txbSenha; 
private JTextField txbNivelAcesso; 
private JTextField txbUltimoAcesso; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       FrmAdmEdit frame = new FrmAdmEdit(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public FrmAdmEdit() { 
    setResizable(false); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 640, 400); 
    this.setLocationRelativeTo(null); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    contentPane.setLayout(new BorderLayout(0, 0)); 
    setContentPane(contentPane); 

    JDesktopPane desktopPane = new JDesktopPane(); 
    desktopPane.setBackground(SystemColor.textInactiveText); 
    contentPane.add(desktopPane, BorderLayout.CENTER); 

    AdminJTable tblListaAdmin = new AdminJTable(); 
    tblListaAdmin.setBounds(10, 76, 604, 274); 
    carregarTabela(tblListaAdmin); 
    desktopPane.add(tblListaAdmin); 

    JLabel lblUsurio = new JLabel("Usu\u00E1rio"); 
    lblUsurio.setBounds(10, 11, 46, 14); 
    desktopPane.add(lblUsurio); 

    txbUsuario = new JTextField(); 
    txbUsuario.setBounds(53, 8, 212, 20); 
    desktopPane.add(txbUsuario); 
    txbUsuario.setColumns(10); 

    JLabel lblSenha = new JLabel("Senha:"); 
    lblSenha.setBounds(283, 11, 46, 14); 
    desktopPane.add(lblSenha); 

    txbSenha = new JPasswordField(); 
    txbSenha.setBounds(323, 8, 139, 20); 
    desktopPane.add(txbSenha); 

    JLabel lblNivelDeAcesso = new JLabel("Nivel de Acesso:"); 
    lblNivelDeAcesso.setBounds(472, 11, 79, 14); 
    desktopPane.add(lblNivelDeAcesso); 

    txbNivelAcesso = new JTextField(); 
    txbNivelAcesso.setBounds(561, 8, 46, 20); 
    desktopPane.add(txbNivelAcesso); 
    txbNivelAcesso.setColumns(10); 

    JLabel lblltimoAcesso = new JLabel("\u00DAltimo Acesso:"); 
    lblltimoAcesso.setBounds(10, 51, 70, 14); 
    desktopPane.add(lblltimoAcesso); 

    txbUltimoAcesso = new JTextField(); 
    txbUltimoAcesso.setBounds(90, 45, 100, 20); 
    desktopPane.add(txbUltimoAcesso); 
    txbUltimoAcesso.setColumns(10); 

    JButton btnInserir = new JButton("Inserir"); 
    btnInserir.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      btnAtualizar.setEnabled(false); <-- dont work.. :(
     } 
    }); 
    btnInserir.setBounds(200, 42, 70, 23); 
    desktopPane.add(btnInserir); 

    JButton btnAtualizar = new JButton("Atualizar"); 
    btnAtualizar.setBounds(283, 42, 79, 23); 
    desktopPane.add(btnAtualizar); 

    JButton btnDeletar = new JButton("Deletar"); 
    btnDeletar.setBounds(372, 42, 79, 23); 
    desktopPane.add(btnDeletar); 

    JButton btnNewButton = new JButton("Salvar"); 
    btnNewButton.setBounds(461, 42, 79, 23); 
    desktopPane.add(btnNewButton); 

    JButton btnNewButton_1 = new JButton("X"); 
    btnNewButton_1.setBounds(550, 42, 57, 23); 
    desktopPane.add(btnNewButton_1); 
} 

public void carregarTabela(AdminJTable tabela){ 
    try{ 
     tabela.load(new AdminCrudDao().listaTodos()); 
    }catch(Exception e){ 
     JOptionPane.showMessageDialog(null, "Não foi possivel carregar o banco de dados"); 
    } 
} 

public void inserirOpcoes(){ 
    btnAtualizar.setEnabled(false); <---- dont work if i call this method 
} 
} 
+1

ですでに利用可能であるレイアウト管理APIを使用して解決することができます愚かなエッジケースを追いかけて、あなたの人生の残りの部分を過ごすことになりますコンストラクタのローカルコンテキストには、このコンテキスト内から直接アクセスすることしかできません。代わりにインスタンスフィールドにしてみてください – MadProgrammer

+2

別の問題:ヌルレイアウトと 'setBounds()'が複雑なGUIを作成する最も簡単で最良の方法のように見えるかもしれませんが、Swing GUIは実行するより深刻な困難を作り出しますそれらを使用するとき。 GUIのサイズが変更されたときにコンポーネントのサイズを変更することはありません。これらのコンポーネントは、拡張または維持するロイヤルウィジェットであり、スクロールペインに配置すると完全に失敗し、元のプラットフォームとは異なるすべてのプラットフォームまたは画面解像度で表示されます。 –

答えて

2

btnAtualizar唯一のコンストラクタのローカルコンテキスト内で定義され、それだけで、このコンテキスト内から直接アクセスすることができます。

btnAtualizar例えば、あなたのクラスのインスタンスフィールド、...

public class FrmAdmEdit extends JFrame { 

private JPanel contentPane; 
private JTextField txbUsuario; 
private JPasswordField txbSenha; 
private JTextField txbNivelAcesso; 
private JTextField txbUltimoAcesso; 

private JButton btnAtualizar; 
を作りますあなたのコンストラクタで

、変更:

JButton btnAtualizar = new JButton("Atualizar"); 

btnAtualizar = new JButton("Atualizar"); 

観察

  • JDesktopPaneをこのように使用されるものではなく、実際に使われることを意図していますJInternalFrame
  • Mユーザ・マニュアルレイアウトは、完全な痛みであり、あなたはより良い `btnAtualizar`のみ内で定義されているのSwing API
+0

ありがとうございました!私はあなたの助けを喜んで! – iwakun