[英]Java LayoutGrid to display another Class
我正在研究一個包含3個Java類的簡單GUI:
1-按鈕
2-屏幕
3-並排顯示屏幕和按鈕。
GUI的概念在某種程度上模仿了此概念,但是僅使用大屏幕和按鈕即可: http : //www2.explorando.com.br/wp-content/uploads/2008/10/b1183-urna-eletronica-brasileira .jpg
對於第三類,我的想法是制作一個1x2的GridLayout並以某種方式在同一Grid上顯示其他兩個類。 我怎樣才能做到這一點?
不知道代碼在這里是否重要,但在這里,類按鈕:
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);
}
}
使用JPanel類: 在此處輸入鏈接描述
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()方法返回的對象一樣使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.