考虑到我在左上角写了"文件",然后如何在右上角添加一个按钮?
public class FileViewer extends JPanel
{
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g)
{
Graphics2D graphic = (Graphics2D)g;
graphic.drawString("HTML File:", 14, 15);
}
}
Run Code Online (Sandbox Code Playgroud)
手工绘制你自己的所有组件将是非常有问题的.
您应该更好地使用为您绘制界面的现有组件.
在这种情况下使用swing你可以在JFrame上使用JFrame和JButton ......
像这样:
替代文字http://img36.imageshack.us/img36/4337/imagen2rrq.png
这会容易得多.
这是此示例的代码.显然只是一种品味.考虑阅读Swing教程
import javax.swing.JFrame;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.Box;
import javax.swing.JPanel;
import java.awt.BorderLayout;
public class SuchCode {
public static void main( String [] args ) {
JFrame frame = new JFrame("How can I... ");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout( panel , BoxLayout.LINE_AXIS );
panel.setLayout( boxLayout );
panel.add( new JLabel("File"));
panel.add( Box.createHorizontalGlue() );
panel.add( new JButton("Button"));
frame.add( panel , BorderLayout.NORTH );
frame.pack();
frame.setVisible( true );
}
}
Run Code Online (Sandbox Code Playgroud)
阅读一下,做一个测试,然后尽可能多地回到这里.我很乐意帮助你(我打赌其他一些不错的java家伙也会这样)
| 归档时间: |
|
| 查看次数: |
2008 次 |
| 最近记录: |