我正在开发一个由3个Java类组成的简单GUI:
1 - 按钮
2 - 屏幕
3 - 并排显示屏幕和按钮。
GUI的概念在某种程度上模仿了这一点,但只有大屏幕和按钮: http://www2.explorando.com.br/wp-content/uploads/2008/10/b1183-urna-eletronica-brasileira.jpg
我对第3课的想法是制作一个1x2的GridLayout,并以某种方式在同一个Grid上显示另外两个类。我怎么能这样做?
不确定代码在这里是否很重要,但在这里,Class Buttons:
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 :(得分: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是一个Container子类,因此您可以像使用getContentPane()meyhod
返回的对象一样使用它