下面列出了怎么用javax.swing.JFrame的API类实例代码及写法,或者点击链接到github查看源代码。
public static void main(String[] args) {
UIManager.put("swing.boldMetal", Boolean.FALSE);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame.setDefaultLookAndFeelDecorated(true);
Toolkit.getDefaultToolkit().setDynamicLayout(true);
System.setProperty("sun.awt.noerasebackground", "true");
try {
UIManager.setLookAndFeel(new MetalLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
System.out.println(
"Metal Look & Feel not supported on this platform. \n"
+ "Program Terminated");
System.exit(0);
}
JFrame frame = new MetalworksFrame();
frame.setVisible(true);
}
private void test() throws Exception {
SunToolkit.createNewAppContext();
EventQueue.invokeAndWait(() -> {
frame = new JFrame();
final JButton b = new JButton();
b.addPropertyChangeListener(evt -> {
if (!SunToolkit.isDispatchThreadForAppContext(b)) {
System.err.println("Wrong thread:" + currentThread());
fail = true;
}
});
frame.add(b);
frame.setSize(100, 100);
frame.setLocationRelativeTo(null);
frame.pack();
});
go.await();
EventQueue.invokeAndWait(() -> {
frame.dispose();
});
}
/**
* Opens a sample frame under the specified skin.
*
* @param skin
* Skin.
*/
private void openSampleFrame(SubstanceSkin skin) {
JFrame sampleFrame = new JFrame(skin.getDisplayName());
sampleFrame.setLayout(new FlowLayout());
JButton defaultButton = new JButton("active");
JButton button = new JButton("default");
JButton disabledButton = new JButton("disabled");
disabledButton.setEnabled(false);
sampleFrame.getRootPane().setDefaultButton(defaultButton);
sampleFrame.add(defaultButton);
sampleFrame.add(button);
sampleFrame.add(disabledButton);
sampleFrame.setVisible(true);
sampleFrame.pack();
sampleFrame.setLocationRelativeTo(null);
sampleFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
sampleFrame.setIconImage(new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR));
SubstanceCortex.RootPaneScope.setSkin(sampleFrame.getRootPane(), skin);
SwingUtilities.updateComponentTreeUI(sampleFrame);
sampleFrame.repaint();
}
public UpdateUIRecursionTest() {
super("UpdateUIRecursionTest");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
String[] listData = {
"First", "Second", "Third", "Fourth", "Fifth", "Sixth"
};
tree = new JTree(listData);
renderer = new DefaultTreeCellRenderer();
getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
tree.setCellRenderer(this);
setVisible(true);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("LineNumberDemo");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JPanel panel = new JPanel();
frame.setContentPane( panel );
panel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
panel.setLayout(new BorderLayout());
JTextPane textPane = new JTextPane();
textPane.setFont( new Font("monospaced", Font.PLAIN, 12) );
textPane.setText("abc");
JScrollPane scrollPane = new JScrollPane(textPane);
panel.add(scrollPane);
scrollPane.setPreferredSize(new Dimension(300, 250));
LineNumber lineNumber = new LineNumber( textPane );
scrollPane.setRowHeaderView( lineNumber );
frame.pack();
frame.setVisible(true);
}
/**
* Starting point of this application.
*
* @param args
* arguments to this application.
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
// instance of this application
AddAndMoveGraphicsApp addGraphicsApp = new AddAndMoveGraphicsApp();
// create the UI, including the map, for the application.
JFrame appWindow = addGraphicsApp.createWindow();
appWindow.add(addGraphicsApp.createUI());
appWindow.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
public static void main(final String[] args)
throws InvocationTargetException, InterruptedException {
SwingUtilities.invokeAndWait(() -> {
frame = new JFrame();
frame.add(new JButton() {
@Override
protected void paintComponent(final Graphics g) {
Graphics gg = new BufferedImage(getWidth(), getHeight(),
BufferedImage.TYPE_INT_ARGB).createGraphics();
super.paintComponent(gg);
gg.dispose();
}
});
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
sleep();
SwingUtilities.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
public ProgressPanel(JFrame owner, String message, boolean modal) {
super(owner, modal);
title = message;
setTitle(message);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
int x = 100;
int y = 100;
if (MainWindow.frame != null) {
x = MainWindow.frame.getX() + MainWindow.frame.getWidth()/2 - (message.length()*9)/2;
y = MainWindow.frame.getY() + MainWindow.frame.getHeight()/2;
} else {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
x = (int) ((dimension.getWidth() - this.getWidth()) / 2);
y = (int) ((dimension.getHeight() - this.getHeight()) / 2);
}
setBounds(100, 100, message.length()*11, 10);
this.setLocation(x, y);
}
/**
* Starting point of this application.
*
* @param args arguments to this application.
*/
public static void main(String[] args) {
// Tip: Use HTTP request intercepter such as Fiddler to track requests
// ProxySetup.setupProxy("localhost", 8888);
// create the UI, including the map, for the application
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
DemoTheatreApp app = new DemoTheatreApp();
JFrame appWindow = app.createWindow();
appWindow.add(app.createUI());
appWindow.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Creates a new instance of RaceGUI
*/
public RaceGUI(String appName) {
UIManager.put("swing.boldMetal", Boolean.FALSE);
JFrame f = new JFrame(appName);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
// Add Track view
track = new TrackView();
f.add(track, BorderLayout.CENTER);
// Add control panel
controlPanel = new RaceControlPanel();
f.add(controlPanel, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
public static void highlight(int x, int y, int width, int height, int time) {
JFrame f = new JFrame();
f.setUndecorated(true);
f.setBackground(new Color(0, 0, 0, 0));
f.setAlwaysOnTop(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setType(JFrame.Type.UTILITY);
f.setFocusableWindowState(false);
f.setAutoRequestFocus(false);
f.setLocation(x, y);
f.setSize(width, height);
f.getRootPane().setBorder(BorderFactory.createLineBorder(Color.RED, 3));
f.setVisible(true);
delay(time);
f.dispose();
}
public static void main(String[] args) throws IrcException, IOException {
Toolkit.getDefaultToolkit().setDynamicLayout(true);
System.setProperty("sun.awt.noerasebackground", "true");
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try {
UIManager.setLookAndFeel("de.muntjak.tinylookandfeel.TinyLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
initUI();
}
/**
*
* @param emp
* An array of employees.
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
private static JFrame printEmployees(final ArrayList<Employee> emp)
throws ParserConfigurationException, SAXException, IOException {
TableEModel m = new TableEModel(emp);
JTable t = new JTable(m);
JPanel panel = new JPanel();
JFrame fram = new JFrame("Employees");
panel.setLayout(new BorderLayout());
JScrollPane tableC = new JScrollPane(t);
tableC.getViewport().add(t);
panel.add(tableC);
fram.getContentPane().add(panel);
fram.pack();
fram.setVisible(true);
return fram;
}
/**
* Constructor ComponentImage
* @param comp
*/
public ComponentImage(Component comp) {
c = comp;
if(comp instanceof JFrame) {
comp = ((JFrame) comp).getContentPane();
} else if(comp instanceof JDialog) {
comp = ((JDialog) comp).getContentPane();
}
image = new BufferedImage(comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
if(comp instanceof Renderable) {
image = ((Renderable) comp).render(image);
} else {
java.awt.Graphics g = image.getGraphics();
comp.paint(g);
g.dispose();
}
}
private void addPressed() {
Container container = getTopLevelAncestor();
DPolicyMappingChooser dPolicyMappingChooser = null;
if (container instanceof JDialog) {
dPolicyMappingChooser = new DPolicyMappingChooser((JDialog) container, title, null);
} else {
dPolicyMappingChooser = new DPolicyMappingChooser((JFrame) container, title, null);
}
dPolicyMappingChooser.setLocationRelativeTo(container);
dPolicyMappingChooser.setVisible(true);
PolicyMapping newPolicyMapping = dPolicyMappingChooser.getPolicyMapping();
if (newPolicyMapping == null) {
return;
}
policyMappings = PolicyMappingsUtil.add(newPolicyMapping, policyMappings);
populate();
selectPolicyMappingInTable(newPolicyMapping);
}
public void componentShown( ComponentEvent cev )
{
JFrame jf = (JFrame) cev.getComponent( );
Rectangle r = jf.getBounds( );
setLocation( r.x, r.y + r.height );
setSize( r.width, 50 );
setVisible( true );
}
/**
* Creates a new combined memory panel object.
*
* @param parent Parent window of the panel.
* @param debugPerspectiveModel Describes the active debugger GUI options.
*/
public CCombinedMemoryPanel(
final JFrame parent, final CDebugPerspectiveModel debugPerspectiveModel) {
super(new BorderLayout());
Preconditions.checkNotNull(parent, "IE01361: Parent argument can not be null");
m_debugPerspectiveModel = Preconditions.checkNotNull(
debugPerspectiveModel, "IE01362: Debug perspective model argument can not be null");
m_debugPerspectiveModel.addListener(m_internalListener);
final JideSplitPane pane = new JideSplitPane(JideSplitPane.HORIZONTAL_SPLIT) {
private static final long serialVersionUID = -1326165812499630343L;
// ESCA-JAVA0025: Workaround for Case 1168
@Override
public void updateUI() {
// Workaround for Case 1168: The mere presence of a JIDE component
// screws up the look and feel.
}
};
pane.setDividerSize(3); // ATTENTION: Part of the Case 1168 workaround
pane.setProportionalLayout(true);
final CMemoryRefreshButtonPanel refreshPanel = new CMemoryRefreshButtonPanel(
parent, debugPerspectiveModel, new InternalRangeProvider(),
new InternalStackRangeProvider());
m_memorySelectionPanel = new CMemorySelectionPanel(parent, debugPerspectiveModel, refreshPanel);
// Create the GUI
pane.addPane(m_memorySelectionPanel);
m_stackPanel = new CStackView(debugPerspectiveModel);
pane.addPane(m_stackPanel);
add(pane);
}
public void componentMoved( ComponentEvent cev )
{
JFrame jf = (JFrame) cev.getComponent( );
Rectangle r = jf.getBounds( );
setLocation( r.x, r.y + r.height );
setSize( r.width, 50 );
}
private Main(final String[] args) {
this.setTitle(TITLE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UITool.centerJFrame(this);
this.createMenu();
this.setVisible(true);
if (args.length > 0) {
final String fileName = args[0];
final File file = new File(fileName);
if (file.exists()) {
try {
if (fileName.endsWith(ClassFile.EXTENTION_CLASS)) {
this.open_ClassFile(file);
} else if (fileName.endsWith(ClassFile.EXTENTION_JAR) || fileName.endsWith(ClassFile.EXTENTION_JMOD)) {
this.open_JarFile(file);
} else {
LOGGER.log(Level.WARNING, "The provided file does not supported: filename={0}", fileName);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to open the file. filename=" + fileName, e);
}
this.exit4Masstestmode();
} else {
LOGGER.log(Level.WARNING, "The provided file does not exist: filename={0}", fileName);
}
}
}
public static void main (String[] args) {
SliderTester t = new SliderTester();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setSize(500, 600);
t.add(SliderTester.getPanel());
t.setVisible(true);
}
public StartScreen(ImageInfoReader imageInfoReaderValue, SoundIdReader soundIdReaderValue, MusicPlayer musicPlayerValue, KeyBindings keyBindings, JFrame parentFrame) {
initialize(parentFrame);
imageInfoReader = imageInfoReaderValue;
soundIdReader = soundIdReaderValue;
musicPlayer = musicPlayerValue;
this.keyBindings = keyBindings;
this.parentFrame = parentFrame;
}
/**
* @see net.sourceforge.squirrel_sql.client.update.UpdateController#showUpdateDialog()
*/
public void showUpdateDialog()
{
final JFrame parent = _app.getMainFrame();
final IUpdateSettings settings = getUpdateSettings();
final boolean isRemoteUpdateSite = settings.isRemoteUpdateSite();
GUIUtils.processOnSwingEventThread(new Runnable()
{
@Override
public void run()
{
UpdateManagerDialog dialog = new UpdateManagerDialog(parent, isRemoteUpdateSite);
if (isRemoteUpdateSite)
{
dialog.setUpdateServerName(settings.getUpdateServer());
dialog.setUpdateServerPort(settings.getUpdateServerPort());
dialog.setUpdateServerPath(settings.getUpdateServerPath());
dialog.setUpdateServerChannel(settings.getUpdateServerChannel());
}
else
{
dialog.setLocalUpdatePath(settings.getFileSystemUpdatePath());
}
dialog.addCheckUpdateListener(UpdateControllerImpl.this);
dialog.setVisible(true);
}
});
}
/**
* The main method for <code>this</code> sample. The arguments are ignored.
*
* @param args
* Ignored.
*/
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(() -> {
SubstanceCortex.GlobalScope.setSkin(new BusinessBlackSteelSkin());
new RegisterTabCloseChangeListener_GeneralMultiple().setVisible(true);
});
}
private static void createAndShowGUI() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBackground(DESKTOPPANE_COLOR);
internalFrame = new JInternalFrame("Test") {
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(FRAME_COLOR);
g.fillRect(0, 0, getWidth(), getHeight());
}
};
internalFrame.setSize(WIN_WIDTH / 3, WIN_HEIGHT / 3);
internalFrame.setVisible(true);
desktopPane.add(internalFrame);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(desktopPane, BorderLayout.CENTER);
frame.add(panel);
frame.setSize(WIN_WIDTH, WIN_HEIGHT);
frame.setVisible(true);
frame.requestFocus();
}
/**
* Class constructor.
*
* @param imgPanel
* @param resultsPath
* @throws HeadlessException
*/
public ImageViewer(ImageTreePanel imgPanel, String resultsPath) throws HeadlessException {
super("Preview");
this.imgPanel = imgPanel;
this.resultsPath = resultsPath;
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
// Create and set up the content pane.
JPanel panel = new JPanel();
JPanel main = new JPanel();
JLabel label = new JLabel("Output format");
String op[] = {"PNG", "JPG", "SVG"};
imgType = new JComboBox(op);
imgType.setSelectedIndex(0);
btn = new JButton("Save all images as...");
btn.addActionListener(this::btnMenuActionPerformed);
panel.add(label);
panel.add(imgType);
panel.add(btn);
main.setLayout(new BorderLayout());
main.add(this.imgPanel, BorderLayout.CENTER);
main.add(panel, BorderLayout.SOUTH);
setContentPane(main);
// Display the window.
pack();
setSize(700, 500);
setVisible(true);
}
/**
* Removes all breakpoints that are part of a given view.
*
* @param parent Parent window used for dialogs.
* @param debuggerProvider Provides the debuggers where breakpoints can be set.
* @param view The view to consider when removing breakpoints.
*/
public static void removeAllView(final JFrame parent,
final BackEndDebuggerProvider debuggerProvider, final INaviView view) {
Preconditions.checkNotNull(parent, "IE01933: Parent argument can't be null");
Preconditions.checkNotNull(debuggerProvider,
"IE02251: Debugger provider argument can not be null");
Preconditions.checkNotNull(view, "IE01956: View argument can't be null");
if (JOptionPane.YES_OPTION == CMessageBox.showYesNoCancelQuestion(parent,
"Do you really want to remove all breakpoints from this view?")) {
for (final IDebugger debugger : debuggerProvider) {
removeAllView(debugger.getBreakpointManager(), view);
}
}
}
private void setupUI() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cb1 = new JComboBox<>(items);
cb2 = new JComboBox<>(items);
JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(cb1);
panel.add(cb2);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
static public void main(String[] s) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
LookAndFeelFactory.installDefaultLookAndFeelAndExtension();
JFrame frame = new JFrame("CheckBoxList Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CheckBoxListDemo());
frame.pack();
frame.setVisible(true);
}
});
}
/**
* Contructs the layout with a container for displaying chart and a control
* panel for selecting interactivity.
*
* @param args
*/
public static void main( String[] args )
{
final Regression_117986_swing siv = new Regression_117986_swing( );
JFrame jf = new JFrame( );
jf.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
jf.addComponentListener( siv );
Container co = jf.getContentPane( );
co.setLayout( new BorderLayout( ) );
co.add( siv, BorderLayout.CENTER );
Dimension dScreen = Toolkit.getDefaultToolkit( ).getScreenSize( );
Dimension dApp = new Dimension( 600, 400 );
jf.setSize( dApp );
jf.setLocation(
( dScreen.width - dApp.width ) / 2,
( dScreen.height - dApp.height ) / 2 );
jf.setTitle( siv.getClass( ).getName( ) + " [device=" //$NON-NLS-1$
+ siv.idr.getClass( ).getName( ) + "]" );//$NON-NLS-1$
ControlPanel cp = siv.new ControlPanel( siv );
co.add( cp, BorderLayout.SOUTH );
siv.idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, siv );
jf.addWindowListener( new WindowAdapter( ) {
public void windowClosing( WindowEvent e )
{
siv.idr.dispose( );
}
} );
jf.setVisible( true );
}
@Override
public void run() {
try {
JFrame f = (JFrame)WindowManager.getDefault ().getMainWindow ();
Component c = f.getGlassPane ();
c.setVisible ( show );
c.setCursor (show ? Cursor.getPredefinedCursor (Cursor.WAIT_CURSOR) : null);
}
catch (NullPointerException npe) {
Exceptions.printStackTrace(npe);
}
}