gpt4 book ai didi

java - ScrollBar 和 JTable 选择的问题

转载 作者:行者123 更新时间:2023-12-01 13:10:50 26 4
gpt4 key购买 nike

问题:

  1. 即使在最大化时,也需要滚动条增加到面板高度。

  2. 当点击1或2时,需要将选择颜色更改为其他颜色。

  3. 单击“x”时,字体应发生变化。

代码:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.Color;
import javax.swing.border.CompoundBorder;
import java.awt.Dimension;

public class Sample1 extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Sample1 frame = new Sample1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Sample1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

final JPanel panel = new JPanel();
panel.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
contentPane.add(panel, BorderLayout.WEST);

Vector str = new Vector<>();
Vector v = new Vector();
v.add("1");
v.add("x");
Vector v1 = new Vector();
v1.add("2");
v1.add("x");
str.addElement(v);
str.addElement(v1);
DefaultTableModel mo = new DefaultTableModel(str,str);
final JTable list = new JTable(mo);
list.setBorder(new CompoundBorder());
list.setBackground(new Color(238, 232, 170));
list.setShowGrid(false);
// list.setListData(str);
list.getColumnModel().getColumn(1).setPreferredWidth(10);
JScrollPane scrollPane = new JScrollPane(list);
scrollPane.setPreferredSize(new Dimension(70, 300));
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
list.setTableHeader(null);
list.setFillsViewportHeight(true);
panel.add(scrollPane);

final JPanel panel_1 = new JPanel();
panel_1.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new CardLayout(0, 0));

JPanel panel_2 = new JPanel();
panel_1.add(panel_2, "1");

JLabel lblNewLabel = new JLabel("First");
panel_2.add(lblNewLabel);

JPanel panel_3 = new JPanel();
panel_1.add(panel_3, "2");

JLabel lblNewLabel_1 = new JLabel("Second");
panel_3.add(lblNewLabel_1);


list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e){
int row = list.getSelectedRow();
int column = list.getSelectedColumn();
CardLayout cl = (CardLayout) panel_1.getLayout();
cl.show(panel_1,list.getValueAt(row, column).toString());
}
});

// list.add
}

}

最佳答案

Need the scrollbar to increase to the panel height even when maximized.

JPanel panel = new JPanel(new BorderLayout());

这将解决您的第一个问题。

关于java - ScrollBar 和 JTable 选择的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22884087/

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