@SuppressWarnings("unused") private boolean isLegal() { try { int row = Integer.parseInt(rowField.getText()); if (row <= 0) { HintFrame hint = new HintFrame( "排数必须为正!", frame.getX(), frame.getY(), frame.getWidth(), frame.getHeight()); return false; } } catch (NumberFormatException e) { HintFrame hint = new HintFrame("排数格式错误!", frame.getX(), frame.getY(), frame.getWidth(), frame.getHeight()); return false; } try { int place = Integer.parseInt(placeField.getText()); if (place <= 0) { HintFrame hint = new HintFrame( "每排位数必须为正!", frame.getX(), frame.getY(), frame.getWidth(), frame.getHeight()); return false; } } catch (NumberFormatException e) { HintFrame hint = new HintFrame( "每排位数格式错误!", frame.getX(), frame.getY(), frame.getWidth(), frame.getHeight()); return false; } return true; }
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D graphics = (Graphics2D) g; graphics.setPaint(Color.GREEN); Ellipse2D.Double circle = new Ellipse2D.Double(ball.getX(), ball.getY(), ball.getDiameter(), ball.getDiameter()); // graphics.fillOval(ball.getX(), ball.getY(), ball.getDiameter(), ball.getDiameter()); graphics.fill(circle); if (frame.getWidth() != frameWidth || frame.getHeight() != frameHeight) { repaint(); updateSizes(frame.getWidth(), frame.getHeight()); } try { Thread.sleep(7); } catch (InterruptedException e) { } if (begin && !grabBall) { moveBall(); } else if (grabBall) { followMouse(); } }
/** * Moves the paddle down by the movement offset field within the picture window's bounds * * @param pict is the Picture object with the boundary to keep the paddle within */ public void moveDown(JFrame pict) { this.y += this.offset; // Move y down by the offset // Test to see if the boundary was in the way below if (this.y + this.height > pict.getHeight()) this.y = pict.getHeight() - this.height; // If so, set bottom to picture bottom }
/** * Moves the paddle down by the movement offset field within the picture window's bounds avoiding * the given ball * * @param pict is the Picture object with the boundary to keep the paddle within * @param ball is a ball that causes the paddle to move back if it's in the way */ public void moveDown(JFrame pict, Ball ball) { this.y += this.offset; // Move y down by the offset // Test to see if the paddle moved into the ball if (ball.distance(this.x, this.y + this.height) < ball.getRadius() || ball.distance(this.x + this.width / 2, this.y + this.height) < ball.getRadius() || ball.distance(this.x + this.width, this.y + this.height) < ball.getRadius()) this.y -= this.offset; // If so, move it out of the ball // Test to see if the boundary was in the way below if (this.y + this.height > pict.getHeight()) this.y = pict.getHeight() - this.height; // If so, set bottom to picture bottom }
public static void pop(String message) { JFrame f = new JFrame(); f.add(new JLabel(message)); f.setAlwaysOnTop(true); f.pack(); f.setVisible(true); final int thisHeight = f.getHeight(); f.addWindowListener( new WindowListener() { public void windowOpened(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { currentHeight = currentHeight - thisHeight; } public void windowClosed(WindowEvent e) {} public void windowActivated(WindowEvent e) {} }); f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); currentHeight = currentHeight + thisHeight; f.setLocation(screenWidth - f.getWidth(), screenHeight - currentHeight); }
// 设置窗体居中 public static void setFrameCenter(JFrame jf) { /* 思路: A:获取屏幕的宽和高 B:获取窗体的宽和高 C:(用屏幕的宽-窗体的宽)/2,(用屏幕的高-窗体的高)/2作为窗体的新坐标。 */ // 获取工具对象 Toolkit tk = Toolkit.getDefaultToolkit(); // 获取屏幕的宽和高 Dimension d = tk.getScreenSize(); double srceenWidth = d.getWidth(); double srceenHeigth = d.getHeight(); // 获取窗体的宽和高 int frameWidth = jf.getWidth(); int frameHeight = jf.getHeight(); // 获取新的宽和高 int width = (int) (srceenWidth - frameWidth) / 2; int height = (int) (srceenHeigth - frameHeight) / 2; // 设置窗体坐标 jf.setLocation(width, height); }
/** Displays the inventory window */ public void inventoryWindow() { if (inventoryWin != null) { inventoryWin.dispose(); } inventoryWin = new JFrame("Inventory"); inventoryWin.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Inventory inv = player.getInventory(); int y = inv.getSize(); if (y == 0) { displayMessage("Inventory is empty"); return; } int cols = 2; int rows = (y - y % cols) / cols; if (y % cols != 0) rows = rows + 1; inventoryWin.setLayout(new GridLayout(0, cols * 2)); for (int i = 0; i < y; i++) { JButton b = new JButton(inv.getItem(i).getName()); inventoryWin.add(b); b.addActionListener(this); JButton d = new JButton("drop " + inv.getItem(i).getName()); inventoryWin.add(d); d.addActionListener(this); } // inventoryWin.setBounds(350, 0, 300, 80*rows); inventoryWin.setLocation(mainWindow.getX(), mainWindow.getY() + mainWindow.getHeight()); inventoryWin.pack(); inventoryWin.setVisible(true); }
synchronized void resetScalingFactors() { double width = frame.getWidth(); double height = frame.getHeight(); scaleFactorWidth = width / res_width; scaleFactorHeight = height / res_height; }
@Override public void streamOpen() { if (isStreamOpen) { L.og("INTERNAL L ERROR: trying to re-open alreadyopen stream "); return; } logFrame = new JFrame("Logging Window"); logArea = new JTextArea(); logArea.setEditable(false); logFrame.setSize(width, height); logFrame.setResizable(false); logFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); JScrollPane logAreaScroller = new JScrollPane(logArea); logAreaScroller.setSize(logFrame.getSize()); logAreaScroller.setPreferredSize( new Dimension((logFrame.getWidth() * 3) / 4, (logFrame.getHeight() * 3) / 4)); logPanel.add(logAreaScroller); logFrame.getContentPane().add(logPanel); logFrame.setVisible(true); isStreamOpen = true; }
private void centraLaVentana() throws HeadlessException { Toolkit toolkit = ventanaPrincipal.getToolkit(); Dimension size = toolkit.getScreenSize(); ventanaPrincipal.setLocation( (size.width - ventanaPrincipal.getWidth()) / 2, (size.height - ventanaPrincipal.getHeight()) / 2); }
public PassWord() { flogin = new JFrame(); panel = new JPanel(); panel.setPreferredSize(new Dimension(400, 300)); panel.setOpaque(false); // 设置透明 flogin.setResizable(false); randomphoto = new random(); ImageIcon titleIcon = new ImageIcon(getClass().getResource("/image/1.jpg")); // 获取图片 randomno = new JLabel(new ImageIcon(randomphoto.creatImage())); ImagePanel jDialogImage = new ImagePanel("/image/2.jpg"); flogin.setContentPane(jDialogImage); // 设置背景图片 jDialogImage.setBounds(0, 0, flogin.getWidth(), flogin.getHeight()); flogin.setIconImage(titleIcon.getImage()); // 设置图标 initframe(); contentPane = flogin.getContentPane(); layout1 = new SpringLayout(); layout2 = new SpringLayout(); add(); contentPane.setLayout(layout1); signIn.addActionListener(this); reset.addActionListener(this); flogin.setBounds(400, 120, 400, 300); flogin.getRootPane().setDefaultButton(signIn); // 设置响应回车键 flogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); flogin.setVisible(true); contentPane.add(panel); }
@Override public void run() { Thread videoDecoder = new Thread(new VideoDecoder(this)); videoDecoder.start(); long lastFrameWrite = System.nanoTime() - NANOSECONDS_PER_FRAME; long timeNow = 0; try { while ((!videoDecoded || !imgBuffer.isEmpty()) && frame.isVisible()) { timeNow = System.nanoTime(); if (timeNow - lastFrameWrite >= NANOSECONDS_PER_FRAME) { lastFrameWrite = timeNow; BufferedImage img = imgBuffer.take(); g.drawImage(img, 0, 0, frame.getWidth(), frame.getHeight(), null); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(img, "jpg", bos); proxy.receiveImage(ByteBuffer.wrap(bos.toByteArray())); } } } catch (IOException | InterruptedException e) { // other user has disconnected, stop sending video } imgBuffer.clear(); frame.setVisible(false); try { proxy.setFrameVisible(true, false); } catch (IOException e) { System.err.println("The other user has disconnected, stopping video."); } }
public static void setFrameCenter(JFrame jf) { // ЙМол Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension screen = toolkit.getScreenSize(); int x = (screen.width - jf.getWidth()) >> 1; int y = (screen.height - jf.getHeight() >> 1) - 32; jf.setLocation(x, y); }
public void paintComponent(Graphics g) { super.paintComponent(g); int width = frame.getWidth(); int height = frame.getHeight(); Point p = new Point(startPoistion.getX(), startPoistion.getY()); glyph.draw(g, p, width, height); }
public void grab(JFrame grabbedFrame) { this.grabbedFrame = grabbedFrame; if (grabbedFrame != null && robot != null) { x = grabbedFrame.getX() + grabbedFrame.getWidth() / 2; y = grabbedFrame.getY() + grabbedFrame.getHeight() / 2; robot.mouseMove(x, y); } }
private void setMiddle() { Point point = owner.getLocationOnScreen(); int x = point.x; int y = point.y; int width = (int) ((owner.getWidth() - this.getWidth()) / 2); int height = (int) ((owner.getHeight() - this.getHeight()) / 2); this.setLocation(x + width, y + height); }
public FrameRenderer(final JFrame target, boolean isFullscreen, RenderFitMode renderFitMode) { m_renderTarget = target; m_renderFitMode = renderFitMode; m_renderTarget.setSize(target.getSize()); m_canvasRenderWidth = target.getWidth(); m_canvasRenderHeight = target.getHeight(); target.setIgnoreRepaint(true); target.createBufferStrategy(2); m_bufferStrategy = target.getBufferStrategy(); BufferCapabilities cap = m_bufferStrategy.getCapabilities(); if (!cap.getBackBufferCapabilities().isAccelerated()) m_logger.warn("Backbuffer is not accelerated, may result in poorer render performance."); if (!cap.isPageFlipping()) m_logger.warn( "Page flipping is not supported by this device. This may result in increased image flicker or tearing."); if (!cap.isMultiBufferAvailable()) m_logger.warn( "Multiple buffering for page flipping is not supported by this device. This may result in poorer performance."); if (cap.isFullScreenRequired() && !isFullscreen) m_logger.warn( "Full screen required for hardware acceleration - not running in fullscreen may cause performance issues."); if (isFullscreen) { enterFullscreen(); target.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { GraphicsDevice device = target.getGraphicsConfiguration().getDevice(); Window fullscreen = device.getFullScreenWindow(); if (fullscreen != target) fullscreen = null; if (fullscreen == null) enterFullscreen(); else exitFullscreen(); } } }); } }
// Paints JPanel in the center of the screen private void paintPanel() { // center the dialog panel1.setLocation( (screen.getWidth() - panel1.getWidth()) / 2, (screen.getHeight() - panel1.getHeight()) / 2); // add the dialog to the "modal dialog" layer of the // screen's layered pane. Container contentPane = screen.getContentPane(); contentPane.add(panel1); }
/** Sets up and displays the GUI for this application. */ public void start() { myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setupComponents(); myFrame.pack(); myFrame.setVisible(true); myFrame.setLocation( SCREEN_SIZE.width / 2 - myFrame.getWidth() / 2, SCREEN_SIZE.height / 2 - myFrame.getHeight() / 2); }
/** Creates new form ViewPrompt */ public ViewPrompt(JFrame f) { this.f = f; setAlwaysOnTop(true); setUndecorated(true); initComponents(); setLocation( f.getX() + (f.getWidth() - getWidth()) / 2, f.getY() + (f.getHeight() - getHeight()) / 2); setVisible(true); }
/** 设置布局居中 */ public static void setLayoutCenter(JFrame frame) { int windowWidth = frame.getWidth(); // 获得窗口宽 int windowHeight = frame.getHeight(); // 获得窗口高 Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包 Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸 int screenWidth = screenSize.width; // 获取屏幕的宽 int screenHeight = screenSize.height; // 获取屏幕的高 frame.setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2); }
public AttributeEditorDialog(JFrame owner, Operator exampleSource) { super(owner, "attribute_editor", true); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(this); DataControl control = new DataControl(0, 0, "Example", "Attribute", false); attributeEditor = new AttributeEditor(exampleSource, control); control.addViewChangeListener(attributeEditor); getContentPane().add(control, BorderLayout.WEST); getContentPane().add(new ExtendedJScrollPane(attributeEditor), BorderLayout.CENTER); control.update(); // initialize actions initActions(); // menu bar JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new ResourceMenu("attribute_editor_file"); fileMenu.add(OPEN_ATTRIBUTE_FILE_ACTION); fileMenu.add(SAVE_ATTRIBUTE_FILE_ACTION); fileMenu.add(LOAD_DATA_ACTION); fileMenu.add(SAVE_DATA_ACTION); fileMenu.add(LOAD_SERIES_DATA_ACTION); fileMenu.addSeparator(); fileMenu.add(CLOSE_ACTION); menuBar.add(fileMenu); JMenu tableMenu = new ResourceMenu("attribute_editor_table"); tableMenu.add(attributeEditor.GUESS_TYPE_ACTION); tableMenu.add(attributeEditor.GUESS_ALL_TYPES_ACTION); tableMenu.add(attributeEditor.REMOVE_COLUMN_ACTION); tableMenu.add(attributeEditor.REMOVE_ROW_ACTION); tableMenu.add(attributeEditor.USE_ROW_AS_NAMES_ACTION); tableMenu.add(CLEAR_ACTION); menuBar.add(tableMenu); setJMenuBar(menuBar); // tool bar JToolBar toolBar = new ExtendedJToolBar(); toolBar.add(OPEN_ATTRIBUTE_FILE_ACTION); toolBar.add(SAVE_ATTRIBUTE_FILE_ACTION); toolBar.add(LOAD_DATA_ACTION); toolBar.add(SAVE_DATA_ACTION); toolBar.addSeparator(); toolBar.add(CLEAR_ACTION); getContentPane().add(toolBar, BorderLayout.NORTH); setSize( (int) Math.max(600, owner.getWidth() * 2.0d / 3.0d), (int) Math.max(400, owner.getHeight() * 2.0d / 3.0d)); setLocationRelativeTo(owner); }
public static void main(String[] args) { JFrame frame = new JFrame("Exercise01"); JApplet applet = new Exercise01(); frame.add(applet); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(640, 480); frame.setMinimumSize(new Dimension(frame.getWidth(), frame.getHeight())); frame.setLocationRelativeTo(null); frame.setVisible(true); }
@SuppressWarnings("deprecation") Runtime(JFrame app, KeyboardInput keyboard, MouseInput mouse) { window = app; window.setCursor(Cursor.CROSSHAIR_CURSOR); this.mouse = mouse; this.keyboard = keyboard; height = window.getHeight() - 20; width = window.getWidth(); }
/** * Constructs a PlatformWindowContextImpl. It starts out by showing a progress window. Later a new * browser window is obtained given the windowId, or created. * * @param openerFrame the opener frame * @param windowId the window id * @param properties the properties */ public NavigatorWindowImpl(NavigatorFrame openerFrame, String windowId, Properties properties) { this.requestedProperties = properties; this.windowId = windowId; WindowFactory wf = windowFactory; if (wf == null) { throw new IllegalStateException("Global WindowFactory is null."); } AbstractBrowserWindow window = wf.getExistingWindow(windowId); FramePanel framePanel = null; if (window != null) { framePanel = window.getTopFramePanel(); if (framePanel == null) { throw new IllegalStateException( "Window with ID " + windowId + " exists but its top frame is null."); } } else { framePanel = FramePanelFactorySource.getInstance().getActiveFactory().createFramePanel(windowId); framePanel.setOpenerFrame(openerFrame); } this.framePanel = framePanel; // Starts out as progress window. // We allow documents to override window properties, but // it can also be the case that such methods as alert() are // invoked while the document loads. if (window != null) { this.browserWindow = window; this.progressWindow = null; this.launched = true; } else { AbstractBrowserWindow newWindow = wf.createWindow(this.windowId, properties, this); this.browserWindow = newWindow; JFrame progressWindow = new ProgressWindow(); this.progressWindow = progressWindow; // Pack to use preferred sizes progressWindow.pack(); // Then resize progressWindow.setSize(new Dimension(400, progressWindow.getHeight())); progressWindow.setLocationByPlatform(true); progressWindow.setVisible(true); progressWindow.addWindowListener( new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { if (!launched) { if (logger.isLoggable(Level.INFO)) { logger.info( "NavigatorWindowImpl(): Disposing browserWindow due to progress window getting closed."); } browserWindow.dispose(); } } }); } }
/** * Centers the dialog above the given frame * * @param frame to center the dialog above * @param dialog to center above the frame */ public static void centerDialog(final JFrame frame, JDialog dialog) { int parWidth = frame.getWidth(); int parHeight = frame.getHeight(); int parX = frame.getLocation().x; int parY = frame.getLocation().y; Point loc = new Point( ((parWidth - dialog.getWidth()) / 2) + parX, ((parHeight - dialog.getHeight()) / 2) + parY); dialog.setLocation(loc); }
public void componentResized(ComponentEvent e) { Component c = e.getComponent(); if (c == mainFrame) { JFrame theMainFrame = (JFrame) c; // update the range of samples to visualize // // // // int newSampleCount = (int)Math.round((theMainFrame.getWidth() - frameWidth) * // samplePerPixel / 2); // // Selection currSel = zoomStack.get(zoomStack.size() - 1); // int newLeftSamples = Math.min(currSel.beginSelInSamples, newSampleCount); // int leftRemainder = newSampleCount - newLeftSamples; // int newRightSamples = Math.min(myHelper.getLengthInFrames() - currSel.endSelInSamples // +1 , newSampleCount); // int rightRemainder = newSampleCount - newRightSamples; // currSel.beginSelInSamples -= newLeftSamples; // currSel.endSelInSamples += newRightSamples; // // // if (leftRemainder > 0 && rightRemainder == 0) { // newRightSamples = Math.min(myHelper.getLengthInFrames() - currSel.endSelInSamples + // 1, leftRemainder); // currSel.endSelInSamples += newRightSamples; // } else if (leftRemainder == 0 && rightRemainder > 0) { // newLeftSamples = Math.min(currSel.beginSelInSamples, rightRemainder); // currSel.beginSelInSamples -= newLeftSamples; // } // // System.err.println(currSel.beginSelInSamples + " " + currSel.endSelInSamples); // selBeginSample = currSel.beginSelInSamples; // selEndSample = currSel.endSelInSamples; // update the visualization area frameHeight = theMainFrame.getHeight(); frameWidth = theMainFrame.getWidth(); // his.overViewHeight = frameHeight / 10; waveHeight = frameHeight - overViewHeight - controlHeight; controlLocY = waveHeight + overViewHeight; zoomThresh = frameWidth / 20; stemThresh = frameWidth / 8; wavePanel.setSize(frameWidth, waveHeight); controlPanel.setBounds(0, controlLocY, frameWidth, controlHeight); overViewPanel.setSize(frameWidth, overViewHeight); wavePanel.refresh( false); // just refresh the view. wait till user calls refresh explicitly to update data overViewPanel.refresh(); // samplePerPixel = (selEndSample - selBeginSample + 1) /frameWidth; mainFrame.validate(); } }
public static void frameInit(JFrame frame) { Toolkit kit2 = Toolkit.getDefaultToolkit(); // 定义工具包 Dimension screenSize = kit2.getScreenSize(); // 获取屏幕的尺寸 DragFrameListener drag = new DragFrameListener(frame); frame.setUndecorated(true); frame.addMouseMotionListener(drag); frame.addMouseListener(drag); int screenWidth = screenSize.width / 2; // 获取屏幕的宽 int screenHeight = screenSize.height / 2; // 获取屏幕的高 int height = frame.getHeight(); int width = frame.getWidth(); frame.setLocation(screenWidth - width / 2, screenHeight - height / 2); }
public static void centerFrame(JFrame frame) { GraphicsDevice defaultScreen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); Rectangle screenSize = defaultScreen.getDefaultConfiguration().getBounds(); int midx = screenSize.width / 2; int midy = screenSize.height / 2; int posx = midx - (frame.getWidth() / 2); int posy = midy - (frame.getHeight() / 2); frame.setLocation(posx, posy); }
/** The main routine simply opens a window that shows a PaintWithOffScreenCanvas panel. */ public static void main(String[] args) { JFrame window = new JFrame("PaintWithOffScreenCanvas"); AdvancedGUIEX1 content = new AdvancedGUIEX1(); window.setContentPane(content); window.setJMenuBar(content.getMenuBar()); window.pack(); window.setResizable(false); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); window.setLocation( (screenSize.width - window.getWidth()) / 2, (screenSize.height - window.getHeight()) / 2); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); }