Почему мой jframe не тот полный размер, который я установил?
package graphq.gui; import java.awt.AlphaComposite; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.List; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import graphq.Handler; import graphq.utils.Tool; public class Canvas { private Rectangle selection; private Point clickPoint; public static void main(String[] args) { new Canvas(); } public Canvas() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } // main frame JFrame frame = new JFrame("Graphq Paint"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Pane()); frame.setSize(1920, 1080); frame.setLocationRelativeTo(null); frame.setVisible(true); // tool box JFrame toolFrame = new JFrame(); JButton pencil = new JButton(); JButton select = new JButton(); toolFrame.add(pencil); toolFrame.add(select); pencil.setSize(64, 64); select.setSize(64, 64); toolFrame.setVisible(true); toolFrame.setSize(128, 64); toolFrame.pack(); // buttons pencil.addActionListener(new pencilClick()); select.addActionListener(new selectClick()); ImageIcon pencilIcon = new ImageIcon("./src/images/pencil.png"); pencil.setIcon(pencilIcon); ImageIcon selectIcon = new ImageIcon("./src/images/select.png"); select.setIcon(selectIcon); } }); } public class pencilClick implements ActionListener { public void actionPerformed(ActionEvent e) { Handler.setSelectedTool(Tool.PENCIL); } } public class selectClick implements ActionListener { public void actionPerformed(ActionEvent e) { Handler.setSelectedTool(Tool.SELECT); } } public class Pane extends JPanel { private List<List<Point>> points; public Pane() { points = new ArrayList<>(25); MouseAdapter ma = new MouseAdapter() { private List<Point> currentPath; @Override public void mousePressed(MouseEvent e) { if (Handler.getSelectedTool() == Tool.PENCIL) { currentPath = new ArrayList<>(25); currentPath.add(e.getPoint()); points.add(currentPath); } if (Handler.getSelectedTool() == Tool.SELECT) { clickPoint = e.getPoint(); selection = null; } } @Override public void mouseDragged(MouseEvent e) { if (Handler.getSelectedTool() == Tool.PENCIL) { Point dragPoint = e.getPoint(); currentPath.add(dragPoint); repaint(); } if (Handler.getSelectedTool() == Tool.SELECT) { Point dragPoint = e.getPoint(); int x = Math.min(clickPoint.x, dragPoint.x); int y = Math.min(clickPoint.y, dragPoint.y); int width = Math.max(clickPoint.x, dragPoint.x) - x; int height = Math.max(clickPoint.y, dragPoint.y) - y; if (selection == null) { selection = new Rectangle(x, y, width, height); } else { selection.setBounds(x, y, width, height); } repaint(); } } @Override public void mouseReleased(MouseEvent e) { if (Handler.getSelectedTool() == Tool.PENCIL) { currentPath = null; } if (Handler.getSelectedTool() == Tool.SELECT) { selection = null; repaint(); } } }; addMouseListener(ma); addMouseMotionListener(ma); } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } protected void paintComponent(Graphics g) { if (Handler.getSelectedTool() == Tool.PENCIL) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); for (List<Point> path : points) { Point from = null; for (Point p : path) { if (from != null) { g2d.drawLine(from.x, from.y, p.x, p.y); } from = p; } } g2d.dispose(); } if (Handler.getSelectedTool() == Tool.SELECT) { super.paintComponent(g); if (selection != null) { g.setColor(UIManager.getColor("List.selectionBackground")); Graphics2D g2d = (Graphics2D) g.create(); g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f)); g2d.fill(selection); g2d.dispose(); g2d = (Graphics2D) g.create(); g2d.draw(selection); g2d.dispose(); } } } } public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }
Что я уже пробовал:
установка размера с помощью
toolFrame.setSize(64, 128);
с помощью
<pre lang="java">toolFrame.pack();и
toolFrame.getContentPane().setSize();
заранее спасибо за вашу помощь!
Richard MacCutchan
Скорее всего, потому что вещи изменяются, когда вы звоните pack
.
Astronomize
Я снял пакет, и он все еще делает это.
Richard MacCutchan
Извините, но угадать, что происходит, невозможно. Вам нужно будет провести еще несколько экспериментов.