gpt4 book ai didi

java - 如何在 JTable 之后添加 JButton

转载 作者:行者123 更新时间:2023-11-29 09:48:42 25 4
gpt4 key购买 nike

我有一个 JTable,其中包含一些类似 SSCCE 的数据:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.*;
import java.awt.Color;
class kanji{
public static void main(String args[]){
JFrame frame = new JFrame("Kanji");
JPanel pane = new JPanel();
JTable table = new JTable();
pane.setLayout(new BorderLayout());
JButton agreg = new JButton("Agregar");
DefaultTableModel model = new DefaultTableModel(get_data(), get_header());
JFrame hk = new JFrame("Historial de Significados");
Image icon = Toolkit.getDefaultToolkit().getImage("JLPT.jpg");
ImageIcon ima = new ImageIcon("JLPT.jpg");
table = new JTable(model){
@Override
public boolean isCellEditable(int row, int col){
switch(col){
case 0:
return false;
case 1:
return false;
case 2:
return true;
default:
return false;
}
}
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return String.class;
case 2:
return Boolean.class;
default:
return Boolean.class;
}
}
};
DefaultTableCellRenderer r = new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setForeground(Color.blue);
setHorizontalAlignment(JLabel.CENTER);
setFont(new Font("Microsoft JhengHei", Font.BOLD, 50));
return this;
}
};
DefaultTableCellRenderer r2 = new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setHorizontalAlignment(JLabel.LEFT);
setFont(new Font("Microsoft JhengHei", Font.BOLD, 13));
return this;
}
};
table.getColumnModel().getColumn(0).setCellRenderer(r);
table.getColumnModel().getColumn(1).setCellRenderer(r2);
TableColumn column = null;
for (int i = 0; i < 3; i++) {
column = table.getColumnModel().getColumn(i);
if (i==0) {
column.setMaxWidth(80);
column.setMinWidth(80);
}
else{
if(i==1){
column.setPreferredWidth(470);
}
else{
column.setMaxWidth(50);
column.setMinWidth(50);
}
}
}
table.setRowHeight(table.getRowHeight()+70);
table.setModel(model);
table.getTableHeader().setReorderingAllowed(false);
JScrollPane scroll = new JScrollPane(table);
pane.add(scroll, BorderLayout.CENTER);
pane.add(agreg, BorderLayout.SOUTH);
frame.add(pane);
frame.setTitle("Historial de Significados");
frame.setSize(1350, 700);
frame.setIconImage(icon);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scroll.setViewportView(table);
}
public static Object [][]get_data(){
Object data[][] = new Object[][]{
{"\u4e00", "Uno, 1", true},
{"\u4e01", "Uno, 1", true},
{"\u4e02", "Uno, 1", true},
{"\u4e03", "Uno, 1", true},
{"\u4e04", "Uno, 1", true},
{"\u4e05", "Uno, 1", true},
{"\u4e06", "Uno, 1", true},
{"\u4e07", "Uno, 1", true},
{"\u4e08", "Uno, 1", true},
{"\u4e09", "Uno, 1", true}
};
return data;
}
public static String []get_header(){
String header [] = new String[]{"KANJI", "SIGNIFICADO", "Agregar"};
return header;
}
}

但我想在创建 JTable 后添加 3 个 JButton,我想要一个不占用所有 JFrame 大小的 JTable,在我的代码中我得到一个 Button,但我希望能够设置按钮边界。

我已经尝试添加一个 JPanel 并再次尝试使用“setPreferredSize”,其中 JScrollPane 不起作用,我的意思是我没有出现,所以我看不到所有表格内容;我还尝试了对我不起作用的“setSize”。

也许我必须制作第二个面板或类似的东西,我希望你能帮助我解决这个问题。

我猜一张图片会说 1,000 多个单词,所以:

这就是我得到的和拥有的以及我想要拥有的

enter image description here

所以用简单的英语来说,我只想知道如何渲染表格并在渲染表格后在底部添加按钮(但我不希望按钮具有所有框架宽度,我希望它们远离表格,而不是在一起,因为我有它们,并有可能改变它们的大小和位置)。

最佳答案

enter image description here

private void createUI() {
JFrame frame = new JFrame();

frame.setLayout(new BorderLayout());

final JTable table = new JTable(10, 4);

JPanel btnPnl = new JPanel(new BorderLayout());
JPanel topBtnPnl = new JPanel(new FlowLayout(FlowLayout.TRAILING));
JPanel bottombtnPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));

topBtnPnl.add(new JButton("Select All"));
bottombtnPnl.add(new JButton("Cancel"));
bottombtnPnl.add(new JButton("Add Selected"));

btnPnl.add(topBtnPnl, BorderLayout.NORTH);
btnPnl.add(bottombtnPnl, BorderLayout.CENTER);

table.getTableHeader().setReorderingAllowed(false);



frame.add(table.getTableHeader(), BorderLayout.NORTH);
frame.add(table, BorderLayout.CENTER);
frame.add(btnPnl, BorderLayout.SOUTH);

frame.setTitle("JTable Example.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

关于java - 如何在 JTable 之后添加 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17225988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com