问题
I'm trying to make a black polygon with 8 points and put it in the center underneath the button. I believe I did everything correctly, but the polygon looks partly smushed and not centered under the button. Could someone explain why it's not working properly?
My overriding panel to create the polygon.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
class OwnJpanel extends JPanel{
public void paintComponent(Graphics g){
int[] xpoints = {10, 20, 25, 25, 20, 10, 5, 5};
int[] ypoints = {10, 25, 20, 27, 37, 37, 27, 20};
g.setColor(Color.black);
g.fillPolygon(xpoints, ypoints, 8);
}
}
My main class:
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main{
public Main(){
JFrame jf = new JFrame();
jf.getContentPane();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500,500);
OwnJpanel oj1 = new OwnJpanel();
JPanel jp1 = new JPanel();
JButton jb1 = new JButton("Click Once");
//jf.getContentPane().add(BorderLayout.CENTER, oj1); (Attempted this to center the polygon but it continues to be stuck in the left corner
oj1.add(jb1);
jf.add(oj1);
jf.setVisible(true);
}
public static void main(String [] args){
new main();
}
}
Lastly, is there a JButton with a cross over the button? So a crossed out button (Like X, over the current JButton "Click Once").
回答1:
First of all call super.paintComponent(g);
in your method.
And don't use absolute coordinates. Define
int centerX=getWidht()/2;
int centerY=getHeight()/2;
Then use the center coordinates to adapt your drawing. e.g. centerX+something, centerY+something as coordinates.
Alternatively you can use g.drawString() passing "X" String and center the string.
回答2:
Okay, so instead of trying to figure out the relationship of the polygon to the button, which would take a quite a bit of work and would involve you having to be able to pre-calculate the required amount of space that the button AND the polygon would require, it would be simpler to "cheat" and make use of an appropriate layout manager to do the work for you...
You should also not rely on "magic" numbers and should rely on what is actually known at runtime, such as the actual width and height of the component

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class PolyTest {
public static void main(String[] args) {
new PolyTest();
}
public PolyTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JButton btn = new JButton("A Button");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(btn, gbc);
frame.add(new PolyPane(), gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PolyPane extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.translate(5, 5);
int width = getWidth() - 10;
int height = getHeight() - 10;
int x[] = new int[]{
0,
width / 10,
width / 2,
width - (width / 10),
width,
width - (width / 4),
width / 4};
int y[] = new int[]{
height - (height / 3),
height / 5,
0,
height / 5,
height - (height / 3),
height,
height};
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x, y, x.length);
g2d.dispose();
}
}
}
来源:https://stackoverflow.com/questions/26210236/why-is-my-fillpolygon-shape-looking-odd-java-gui