Java 如何在网格布局中显示文本?
我在JFrame中有一个网格作为我的输出,我正在尝试找出如何在网格左侧垂直显示文本,如a、B、C等,在顶部显示1、2、3等 这是我的输出: 这是代码:Java 如何在网格布局中显示文本?,java,grid-layout,Java,Grid Layout,我在JFrame中有一个网格作为我的输出,我正在尝试找出如何在网格左侧垂直显示文本,如a、B、C等,在顶部显示1、2、3等 这是我的输出: 这是代码: import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class battleShips extends JFrame{ /** * */ private stat
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class battleShips extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JButton applyButton = new JButton("New Game");
GridLayout experimentLayout = new GridLayout(0,6);
public battleShips(String name) {
super(name);
setResizable(false);
}
public void addComponentsToPane(final Container pane) {
final JPanel compsToExperiment = new JPanel();
compsToExperiment.setLayout(experimentLayout);
JPanel controls = new JPanel();
controls.setLayout(new GridLayout(2,3));
//Set up components preferred size
JButton b = new JButton("Just fake button");
Dimension buttonSize = b.getPreferredSize();
compsToExperiment.setPreferredSize(new Dimension((int)(buttonSize.getWidth() * 2.5)+5,
(int)(buttonSize.getHeight() * 3.5)+50*2));
//Add buttons to experiment with Grid Layout
for (int i = 0; i<36; i++)
{
compsToExperiment.add(new JButton("~"));
}
//Add controls to set up horizontal and vertical gaps
controls.add(applyButton);
//Process the Apply gaps button press
applyButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Set up the layout of the buttons
experimentLayout.layoutContainer(compsToExperiment);
}
});
pane.add(compsToExperiment, BorderLayout.NORTH);
pane.add(new JSeparator(), BorderLayout.CENTER);
pane.add(controls, BorderLayout.SOUTH);
}
private static void createAndShowGUI() {
//Create and set up the window.
battleShips frame = new battleShips("Battleships");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
frame.addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
/* Use an appropriate Look and Feel */
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
import java.util.*;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共级战列舰扩展JFrame{
/**
*
*/
私有静态最终长serialVersionUID=1L;
JButton applyButton=新JButton(“新游戏”);
GridLayout=新的GridLayout(0,6);
公共战舰(字符串名称){
超级(姓名);
可设置大小(假);
}
公共void addComponentsToPane(最终容器窗格){
最终JPanel CompstoExperience=新JPanel();
康普斯托实验。设置布局(实验布局);
JPanel controls=新的JPanel();
setLayout(新的GridLayout(2,3));
//设置组件的首选大小
JButton b=新的JButton(“只是假按钮”);
维度按钮大小=b.getPreferredSize();
compstoexperience.setPreferredSize(新维度((int)(buttonSize.getWidth()*2.5)+5,
(int)(buttonSize.getHeight()*3.5)+50*2);
//添加按钮以试验网格布局
对于(inti=0;i可能是这样的
封装gnu;
导入java.util.*;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类主框架{
/**
*
*/
私有静态最终长serialVersionUID=1L;
JButton applyButton=新JButton(“新游戏”);
GridLayout=新的GridLayout(0,6);
公用主(字符串名称){
超级(姓名);
可设置大小(假);
}
公共void addComponentsToPane(最终容器窗格){
最终JPanel CompstoExperience=新JPanel();
康普斯托实验。设置布局(实验布局);
JPanel controls=新的JPanel();
setLayout(新的GridLayout(2,3));
//设置组件的首选大小
JButton b=新的JButton(“只是假按钮”);
维度按钮大小=b.getPreferredSize();
compstoexperience.setPreferredSize(新维度((int)(buttonSize.getWidth()*2.5)+5,
(int)(buttonSize.getHeight()*3.5)+50*2);
//添加按钮以试验网格布局
对于(int i=1;i<7;i++){
如果(i==1)compsToExperiment.add(新标签(“”));
添加(新标签(“+(i-1));
}
对于(int i=0;i<30;i++){
如果(i==0)compsToExperiment.add(新标签(“A”);
否则如果(i==6)compsToExperiment.add(新标签(“B”);
否则,如果(i==12)compsToExperiment.add(新标签(“C”);
否则,如果(i==18)compsToExperiment.add(新标签(“D”);
否则如果(i==24)compsToExperiment.add(新标签(“E”);
else compsToExperiment.add(新的JButton(“~”);
}
//添加控件以设置水平和垂直间隙
控件。添加(applyButton);
//按“应用间隙”按钮进行处理
addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
//设置按钮的布局
实验布局。布局容器(CompstoExperience);
}
});
添加(康普斯托实验,BorderLayout.NORTH);
添加(新的jsepator(),BorderLayout.CENTER);
添加(控件,BorderLayout.SOUTH);
}
私有静态void createAndShowGUI(){
//创建并设置窗口。
主框架=新主框架(“战列舰”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置内容窗格。
frame.addComponentsToPane(frame.getContentPane());
//显示窗口。
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args){
/*使用合适的外观和感觉*/
试一试{
//UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel”);
setLookAndFeel(“javax.swing.plaf.metal.MetalLookAndFeel”);
}捕获(无支持的LookandFeelexception ex){
例如printStackTrace();
}捕获(非法访问例外){
例如printStackTrace();
}catch(实例化异常){
例如printStackTrace();
}捕获(ClassNotFoundException ex){
例如printStackTrace();
}
/*关闭metal对粗体字体的使用*/
UIManager.put(“swing.boldMetal”,Boolean.FALSE);
//为事件分派线程计划作业:
//创建并显示此应用程序的GUI。
javax.swing.SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGUI();
}
});
}
}
只是想知道,按下其中一个“~”它会改变符号,你是怎么做到的?@KingF“~”的改变符号可以在更新视图时完成,我想你可以使用repaint()
方法。你可以用谷歌搜索它,或者在这里搜索gridlayout swing update view或类似的关键字,或者问一个新问题。
package gnu;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton applyButton = new JButton("New Game");
GridLayout experimentLayout = new GridLayout(0, 6);
public Main(String name) {
super(name);
setResizable(false);
}
public void addComponentsToPane(final Container pane) {
final JPanel compsToExperiment = new JPanel();
compsToExperiment.setLayout(experimentLayout);
JPanel controls = new JPanel();
controls.setLayout(new GridLayout(2, 3));
//Set up components preferred size
JButton b = new JButton("Just fake button");
Dimension buttonSize = b.getPreferredSize();
compsToExperiment.setPreferredSize(new Dimension((int) (buttonSize.getWidth() * 2.5) + 5,
(int) (buttonSize.getHeight() * 3.5) + 50 * 2));
//Add buttons to experiment with Grid Layout
for (int i = 1; i < 7; i++) {
if(i==1) compsToExperiment.add(new Label(""));
else compsToExperiment.add(new Label("" + (i-1) ));
}
for (int i = 0; i < 30; i++) {
if (i == 0) compsToExperiment.add(new Label("A"));
else if (i == 6) compsToExperiment.add(new Label("B"));
else if (i == 12) compsToExperiment.add(new Label("C"));
else if (i == 18) compsToExperiment.add(new Label("D"));
else if (i == 24) compsToExperiment.add(new Label("E"));
else compsToExperiment.add(new JButton("~"));
}
//Add controls to set up horizontal and vertical gaps
controls.add(applyButton);
//Process the Apply gaps button press
applyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Set up the layout of the buttons
experimentLayout.layoutContainer(compsToExperiment);
}
});
pane.add(compsToExperiment, BorderLayout.NORTH);
pane.add(new JSeparator(), BorderLayout.CENTER);
pane.add(controls, BorderLayout.SOUTH);
}
private static void createAndShowGUI() {
//Create and set up the window.
Main frame = new Main("Battleships");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
frame.addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
/* Use an appropriate Look and Feel */
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}