Вы стреляете в ногу, используя нулевые макеты и setBounds.
В то время как пустые макеты и setBounds()
могут показаться Swing новичками, как самый простой и лучший способ создания сложных графических интерфейсов, чем больше Swing GUI вы создадите более серьезные трудности, с которыми вы столкнетесь при их использовании. Они не будут изменять размеры ваших компонентов при изменении размера графического интерфейса, они являются королевской ведьмой для улучшения или поддержки, они полностью не выполняются при размещении в scrollpanes, они выглядят ужасно ужасно при просмотре на всех платформах или разрешениях экрана, отличных от исходного ,
Если вы установите границы JTextArea, вы полностью не можете расширять его, не позволяя работать с вашими прокрутками. Вместо этого задайте свойства столбца и строки JTextArea, избавьтесь от макетов null
и используйте менеджеров компоновки, чтобы создавать красивые и легко отлаживаемые графические интерфейсы.
например,
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.*;
public class CartEg extends JPanel {
private static final long serialVersionUID = 1L;
private static final int GAP = 10;
private static final String TITLE_TEXT = "Some Great Title";
private JTextArea itemArea = new JTextArea(25, 60);
public CartEg() {
// create centered title JLabel
JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
// make it big and bold
titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 42f));
itemArea.setWrapStyleWord(true);
itemArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(itemArea);
JPanel bottomButtonPanel = new JPanel(); // panel to hold JButtons
bottomButtonPanel.setLayout(new BoxLayout(bottomButtonPanel, BoxLayout.LINE_AXIS));
bottomButtonPanel.add(new JButton("Left Button"));
bottomButtonPanel.add(Box.createHorizontalGlue());
bottomButtonPanel.add(new JButton("Right Button 1"));
bottomButtonPanel.add(new JButton("Right Button 2"));
// create some empty space around our components
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
// use a BorderLayout for the main JPanel
setLayout(new BorderLayout(GAP, GAP));
// add JScrollPane to the BorderLayout.CENTER position
// add your JPanel that holds buttons to the BorderLayout.PAGE_END
// and add your title JLabel to the BorderLayout.PAGE_START position
add(scrollPane, BorderLayout.CENTER);
add(titleLabel, BorderLayout.PAGE_START);
add(bottomButtonPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
CartEg mainPanel = new CartEg();
JFrame frame = new JFrame("Cart Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Обратите внимание, что создать JTextArea с номером строки и столбца, переданных в конструктор. Затем я настроил JTextArea для обертывания слов. Я добавляю его в JScrollPane, а затем добавляю JScrollPane в положение BorderLayout.CENTER контейнера BorderLayout.
ред пример, который теперь использует AbstractAction, DisposeAction, и использует то же самое действие в обоих JButton и JMenuItem. Это позволяет при необходимости создавать JButton.
создать переменную DisposeAction и инициализировать его:
// create an Action that can be added to a JButton or a JMenuItem
private Action disposeAction = new DisposeAction("Exit", KeyEvent.VK_X);
кнопку, которая использует действие Создать и добавить к GUI одновременно:
// create jbutton that uses our dispose Action
bottomButtonPanel.add(new JButton(disposeAction));
Создание JMenuItem, который использует действие и добавить в JMenu одновременно :
// add a JMenuItem that uses the same disposeAction
fileMenu.add(new JMenuItem(disposeAction));
Вот код для частного внутреннего класса DisposeAction. Он немного запутан, так что он будет работать как для JButtons, так и для JMenuItems. Обратите внимание, что это может быть автономным, при желании, и он будет работать нормально:
private class DisposeAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public DisposeAction(String name, int mnemonic) {
super(name); // set button's text
putValue(MNEMONIC_KEY, mnemonic); // set it's mnemonic key
}
@Override
public void actionPerformed(ActionEvent e) {
// get the button that caused this action
Object source = e.getSource();
if (source instanceof AbstractButton) {
AbstractButton exitButton = (AbstractButton) source;
// get the parent top level window
Window topWindow = SwingUtilities.getWindowAncestor(exitButton);
if (topWindow == null) { // if null, then likely in a JMenuItem
// so we have to get its jpopupmenu parent
Container parent = exitButton.getParent();
if (parent instanceof JPopupMenu) {
JPopupMenu popupMenu = (JPopupMenu) parent;
// get the invoker for the pop up menu
Component invoker = popupMenu.getInvoker();
if (invoker != null) {
// and get *its* top level window
topWindow = SwingUtilities.getWindowAncestor(invoker);
}
}
}
if (topWindow != null) {
// dispose of the top-level window
topWindow.dispose();
}
}
}
}
Полный пример:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class CartEg extends JPanel {
private static final long serialVersionUID = 1L;
private static final int GAP = 10;
private static final String TITLE_TEXT = "Some Great Title";
private JTextArea itemArea = new JTextArea(25, 60);
// create an Action that can be added to a JButton or a JMenuItem
private Action disposeAction = new DisposeAction("Exit", KeyEvent.VK_X);
private JMenuBar menuBar = new JMenuBar(); // menu bar for GUI
public CartEg() {
// create centered title JLabel
JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
// make it big and bold
titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 42f));
itemArea.setWrapStyleWord(true);
itemArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(itemArea);
JPanel bottomButtonPanel = new JPanel(); // panel to hold JButtons
bottomButtonPanel.setLayout(new BoxLayout(bottomButtonPanel, BoxLayout.LINE_AXIS));
bottomButtonPanel.add(new JButton("Left Button"));
bottomButtonPanel.add(Box.createHorizontalGlue());
bottomButtonPanel.add(new JButton("Right Button 1"));
bottomButtonPanel.add(new JButton("Right Button 2"));
// create jbutton that uses our dispose Action
bottomButtonPanel.add(new JButton(disposeAction));
// create some empty space around our components
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
// use a BorderLayout for the main JPanel
setLayout(new BorderLayout(GAP, GAP));
// add JScrollPane to the BorderLayout.CENTER position
// add your JPanel that holds buttons to the BorderLayout.PAGE_END
// and add your title JLabel to the BorderLayout.PAGE_START position
add(scrollPane, BorderLayout.CENTER);
add(titleLabel, BorderLayout.PAGE_START);
add(bottomButtonPanel, BorderLayout.PAGE_END);
// flesh out our jmenubar with a JMenu
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F); // alt-F to invoke it
// add a JMenuItem that uses the same disposeAction
fileMenu.add(new JMenuItem(disposeAction));
menuBar.add(fileMenu);
}
// expose the JMenuBar to the world
public JMenuBar getMenuBar() {
return menuBar;
}
private class DisposeAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public DisposeAction(String name, int mnemonic) {
super(name); // set button's text
putValue(MNEMONIC_KEY, mnemonic); // set it's mnemonic key
}
@Override
public void actionPerformed(ActionEvent e) {
// get the button that caused this action
Object source = e.getSource();
if (source instanceof AbstractButton) {
AbstractButton exitButton = (AbstractButton) source;
// get the parent top level window
Window topWindow = SwingUtilities.getWindowAncestor(exitButton);
if (topWindow == null) { // if null, then likely in a JMenuItem
// so we have to get its jpopupmenu parent
Container parent = exitButton.getParent();
if (parent instanceof JPopupMenu) {
JPopupMenu popupMenu = (JPopupMenu) parent;
// get the invoker for the pop up menu
Component invoker = popupMenu.getInvoker();
if (invoker != null) {
// and get *its* top level window
topWindow = SwingUtilities.getWindowAncestor(invoker);
}
}
}
if (topWindow != null) {
// dispose of the top-level window
topWindow.dispose();
}
}
}
}
private static void createAndShowGui() {
CartEg mainPanel = new CartEg();
JFrame frame = new JFrame("Cart Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.setJMenuBar(mainPanel.getMenuBar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
См редактирует, чтобы ответить. –