2014-11-21 2 views
0

Я работаю на простой графический интерфейс, который состоит из 3 Java классов:Java LayoutGrid для отображения другого класса

1 - кнопки
2 - экран
3 - Отображает экран и боковые кнопки на стороне ,

Концепция GUI является в некоторой степени имитируют, но только с большим экраном и кнопками: http://www2.explorando.com.br/wp-content/uploads/2008/10/b1183-urna-eletronica-brasileira.jpg

Моя идея для 3-го класса должен был сделать GridLayout из 1х2 и как-то отобразить два других классы на одной и той же сетке. Как я могу это сделать?

Не уверен, что если код важен здесь, но здесь идет, Класс кнопки: прямо сейчас

package apresentacao; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class TelaUrna extends JFrame{ 
    private JButton tecla1; 
    private JButton tecla2; 
    private JButton tecla3; 
    private JButton tecla4; 
    private JButton tecla5; 
    private JButton tecla6; 
    private JButton tecla7; 
    private JButton tecla8; 
    private JButton tecla9; 
    private JButton tecla0; 
    private JButton teclaBranco; 
    private JButton teclaCorrige; 
    private JButton teclaConfirma; 
    private JButton teclaVazia; 
    private JButton teclaVazia2; 

public TelaUrna() { 

    //instanciar os componentes!!! 
    tecla1 = new JButton(); 
    tecla2 = new JButton(); 
    tecla3 = new JButton(); 
    tecla4 = new JButton(); 
    tecla5 = new JButton(); 
    tecla6 = new JButton(); 
    tecla7 = new JButton(); 
    tecla8 = new JButton(); 
    tecla9 = new JButton(); 
    tecla0 = new JButton(); 
    teclaBranco = new JButton(); 
    teclaCorrige = new JButton(); 
    teclaConfirma = new JButton(); 
    teclaVazia = new JButton(); 
    teclaVazia2 = new JButton(); 

    //configura o container 
    Container container = getContentPane(); 
    container.setLayout(new GridLayout(5, 3)); 

    //configurar os componentes 
    tecla1.setText("1"); 
    tecla1.setForeground(Color.WHITE); 
    tecla1.setBackground(Color.BLACK); 
    tecla2.setText("2"); 
    tecla2.setForeground(Color.WHITE); 
    tecla2.setBackground(Color.BLACK); 
    tecla3.setText("3"); 
    tecla3.setForeground(Color.WHITE); 
    tecla3.setBackground(Color.BLACK); 
    tecla4.setText("4"); 
    tecla4.setForeground(Color.WHITE); 
    tecla4.setBackground(Color.BLACK); 
    tecla5.setText("5"); 
    tecla5.setForeground(Color.WHITE); 
    tecla5.setBackground(Color.BLACK); 
    tecla6.setText("6"); 
    tecla6.setForeground(Color.WHITE); 
    tecla6.setBackground(Color.BLACK); 
    tecla7.setText("7"); 
    tecla7.setForeground(Color.WHITE); 
    tecla7.setBackground(Color.BLACK); 
    tecla8.setText("8"); 
    tecla8.setForeground(Color.WHITE); 
    tecla8.setBackground(Color.BLACK); 
    tecla9.setText("9"); 
    tecla9.setForeground(Color.WHITE); 
    tecla9.setBackground(Color.BLACK); 
    tecla0.setText("0"); 
    tecla0.setForeground(Color.WHITE); 
    tecla0.setBackground(Color.BLACK); 
    teclaBranco.setText("BRANCO"); 
    teclaBranco.setBackground(Color.WHITE); 
    teclaCorrige.setText("CORRIGE"); 
    teclaCorrige.setBackground(Color.RED); 
    teclaConfirma.setText("CONFIRMA"); 
    teclaConfirma.setBackground(Color.GREEN); 
    teclaVazia.setText(""); 
    teclaVazia2.setText(""); 

    //setar acoes 

    //adicionar no Container, Pack, VisibleTrue, Centralizar e controle da saida 
    container.add(tecla1); 
    container.add(tecla2); 
    container.add(tecla3); 
    container.add(tecla4); 
    container.add(tecla5); 
    container.add(tecla6); 
    container.add(tecla7); 
    container.add(tecla8); 
    container.add(tecla9); 
    container.add(teclaVazia); 
    container.add(tecla0); 
    container.add(teclaVazia2); 
    container.add(teclaBranco); 
    container.add(teclaCorrige); 
    container.add(teclaConfirma); 
    pack(); 
    setLocationRelativeTo(null); 
    setVisible(true); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //tratar eventos 


} 
private class GerenciadorBotoes implements ActionListener { 
    @Override 
    public void actionPerformed(ActionEvent ae) { 

    } 

} 
} 

класса экранов, которые просто кинопробы

package apresentacao; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class TelaVisual extends JFrame { 
    private JLabel teste = new JLabel(); 

    public TelaVisual() { 
    Container container = getContentPane(); 
    container.setLayout(new GridLayout(5, 3)); 

    teste.setText("testando"); 

    container.add(teste); 
    pack(); 
    setLocationRelativeTo(null); 
    setVisible(true); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

} 

ответ

0

Использование JPanel Класс: enter link description here

public class Urna extends JFrame { 
    public Urna() { 
     Container container = getContentPane(); 
     container.setLayout(<some layout manager that fits your needs>); 
     container.add(new TelaVisual()); 
     container.add(new TelaUrna()); 

     pack(); 
     setLocationRelativeTo(null); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 

public class TelaVisual extends JPanel { 
    private JLabel teste = new JLabel(); 

    public TelaVisual() { 
     this.setLayout(new GridLayout(5, 3)); 

     teste.setText("testando"); 

     this.add(teste); 
    } 
} 

public class TelaUrna extends JPanel { 
    ... 
    ... 
    ... 

    public TelaUrna() { 
     // your code here 
     // instead of container.something, use this.something 
    } 
} 

Имейте в виду, что JPanel является подклассом контейнера, поэтому вы можете использовать это точно так же, как вы делали с объектом, возвращаемым getContentPane() meyhod

+0

Большое спасибо человеку, прибили его! : D – leofontes

+0

Ты добрый – ylima

Смежные вопросы