Swing界面问题求助:JScrollPane不显示、JTextArea内嵌按钮失效
解决你的Swing界面两个问题
嘿,我来帮你搞定这两个Swing界面的小问题,咱们一个一个捋清楚:
问题1:JScrollPane未显示
原因分析
你遇到的滚动面板不显示的问题,主要踩了两个Swing布局的坑:
- 给
informationDisplay这个JTextArea设置了setPreferredSize,这会强制文本区域固定大小,哪怕内容超出也不会触发滚动逻辑; - 给
scrollbar1设置了setSize(20,20),这个极小的尺寸会让滚动面板几乎隐身,而且在BorderLayout布局下,手动设置size基本没用,布局管理器会直接覆盖这个设置。
修复方案
- 删掉
informationDisplay.setPreferredSize(...)和scrollbar1.setSize(...)这两行; - 给JTextArea开启自动换行,避免不必要的横向滚动条(提升用户体验);
- 可以给
infoPanel设置一个合适的PreferredSize,用来控制底部信息栏的高度。
修复后的代码片段:
JPanel infoPanel = new JPanel(); infoPanel.setLayout(new BorderLayout()); // 设置底部信息栏的固定高度 infoPanel.setPreferredSize(new Dimension(getWidth(), 200)); JTextArea informationDisplay= new JTextArea(); // 开启自动换行+单词换行,避免内容溢出横向滚动 informationDisplay.setLineWrap(true); informationDisplay.setWrapStyleWord(true); JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20)); informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n"); infoPanel.add(scrollbar1); add(infoPanel, BorderLayout.SOUTH);
问题2:unitButtonPanel中的按钮不显示
原因分析
JTextArea是纯文本显示组件,它的add()方法是继承自Container的遗留设计,实际不会渲染添加进去的其他Swing组件(比如JPanel、JButton)。你的需求是展示按钮面板,完全不需要用JTextArea,换成普通的JPanel就行。
修复方案
把availableUnits的类型从JTextArea改成JPanel,直接把按钮面板加进去就可以正常显示了。
修复后的代码片段:
JPanel textPanel = new JPanel(); textPanel.setLayout(new GridLayout(3, 1, 4, 4)); // 把JTextArea替换成JPanel,用来承载按钮面板 JPanel availableUnits = new JPanel(); availableUnits.setLayout(new BorderLayout()); // 让按钮面板居中显示 unitButtonPanel = new JPanel(); unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2)); ambulanceUnit = new JButton("Ambulance"); diseaseControlUnit = new JButton ("diseaseControlUnit"); gasControlUnit = new JButton("gasControlUnit"); fireTruck = new JButton("fireTruck"); unitButtonPanel.add(ambulanceUnit); unitButtonPanel.add(diseaseControlUnit); unitButtonPanel.add(gasControlUnit); unitButtonPanel.add(fireTruck); availableUnits.add(unitButtonPanel); textPanel.add(availableUnits);
完整修复后的可编译代码
import java.awt.*; import javax.swing.*; public class GameView extends JFrame { private JPanel buttonPanel; private JPanel infoPanel; private JTextArea informationDisplay; private JPanel availableUnits; // 改为JPanel private JPanel unitButtonPanel; private JPanel disasterPanel; private JTextArea disasterDisplay; private JButton ambulanceUnit; private JButton diseaseControlUnit; private JButton gasControlUnit; private JButton fireTruck; public static void main(String[] args) { new GameView(); } public GameView() { setSize(1000, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension dim = tk.getScreenSize(); int xPos = (dim.width/2) - (this.getWidth()/2); int yPos = (dim.height/2) - (this.getHeight()/2); this.setLocation(xPos,yPos); JPanel title = new JPanel(); JLabel label1 = new JLabel("Invaded City"); title.add(label1); add(title, BorderLayout.NORTH); buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(10, 10, 2, 2)); for (int i = 0; i < 100; i++) { JButton mapButton = new JButton(); addButton(mapButton); } add(buttonPanel, BorderLayout.CENTER); JPanel textPanel = new JPanel(); textPanel.setLayout(new GridLayout(3, 1, 4, 4)); // 替换JTextArea为JPanel availableUnits = new JPanel(); availableUnits.setLayout(new BorderLayout()); unitButtonPanel = new JPanel(); unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2)); ambulanceUnit = new JButton("Ambulance"); diseaseControlUnit = new JButton ("diseaseControlUnit"); gasControlUnit = new JButton("gasControlUnit"); fireTruck = new JButton("fireTruck"); unitButtonPanel.add(ambulanceUnit); unitButtonPanel.add(diseaseControlUnit); unitButtonPanel.add(gasControlUnit); unitButtonPanel.add(fireTruck); availableUnits.add(unitButtonPanel); textPanel.add(availableUnits); JTextArea respondingUnits = new JTextArea(2,2); respondingUnits.setLineWrap(true); respondingUnits.setWrapStyleWord(true); textPanel.add(respondingUnits); JTextArea treatingUnits = new JTextArea(2,2); treatingUnits.setLineWrap(true); treatingUnits.setWrapStyleWord(true); textPanel.add(treatingUnits); add(textPanel, BorderLayout.EAST); infoPanel = new JPanel(); infoPanel.setLayout(new BorderLayout()); infoPanel.setPreferredSize(new Dimension(getWidth(), 200)); informationDisplay= new JTextArea(); informationDisplay.setLineWrap(true); informationDisplay.setWrapStyleWord(true); JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20)); informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n"); infoPanel.add(scrollbar1); add(infoPanel, BorderLayout.SOUTH); disasterPanel = new JPanel(); disasterPanel.setLayout(new BorderLayout()); disasterDisplay = new JTextArea(); disasterDisplay.setLineWrap(true); disasterDisplay.setWrapStyleWord(true); disasterDisplay.setPreferredSize(new Dimension(200, getHeight())); // 调整西面板宽度 disasterPanel.add(disasterDisplay); add(disasterPanel, BorderLayout.WEST); pack(); setVisible(true); } public void addButton(JButton b) { buttonPanel.add(b); } }
另外我还给其他JTextArea也加上了自动换行,同时调整了西面板的宽度,让整体布局更协调,你可以根据需求再微调细节~
内容的提问来源于stack exchange,提问作者HR1
相关产品推荐
相关产品推荐

