這篇文章介紹Java介面程式設計建立一個可以畫出圖形的簡單框架的方法
引子:總共使用3個.java文件,建立一個簡單介面程式設計的框架。
第1個檔案:NotHelloWorldComponent.java
//NotHelloWorldComponent.java import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class NotHelloWorldComponent extends JComponent { public static final int MESSAGE_X = 75; //消息最左上角坐标 public static final int MESSAGE_Y = 75; //定义此界面的默认尺寸 private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 300; //paintComponent()会被系统自动调用,并且系统会自动传递一个Graphics对象给它 public void paintComponent(Graphics g){ //将g转型成Graphics2D对象,作为画笔,用来画2D图形 Graphics2D g2 = (Graphics2D)g; //创建一个Rectangle2D矩形对象 //如果有其他图形需要绘制,可以先在此处定义,再在下个部分用g2作为画笔画出来 double leftX = 50; double topY = 50; double width = 200; double height = 150; Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); //用画笔g2来绘制文字和矩形图形 g2.drawString("Not a hello, World program", MESSAGE_X, MESSAGE_Y); g2.draw(rect); } //覆盖此函数,设置首选的尺寸大小 @Override public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }
第2個.java檔案:NotHelloWorldFrame.java
import javax.swing.JFrame; public class NotHelloWorldFrame extends JFrame { public NotHelloWorldFrame(){ add(new NotHelloWorldComponent()); pack(); //使JFrame的大小适合其包含的组件的大小 } }
說明:JFrame是一個框架類,用來包含JPanel和JComponent。
第3個.java檔:NotHelloWorld.java
import java.awt.EventQueue; import javax.swing.JFrame; public class NotHelloWorld { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { //使用匿名内部类技术,创建一个线程,用线程来创建窗口程序 public void run() { JFrame frame = new NotHelloWorldFrame(); frame.setTitle("NotHelloWorldFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口的同时,程序结束,避免内存垃圾 frame.setVisible(true); //开启窗口可见 } }); } }
以上是Java介面程式設計建立一個可以畫出圖形的簡單框架的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

Java代碼在不同平台上運行時會有性能差異。 1)JVM的實現和優化策略不同,如OracleJDK和OpenJDK。 2)操作系統的特性,如內存管理和線程調度,也會影響性能。 3)可以通過選擇合適的JVM、調整JVM參數和代碼優化來提升性能。

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑戰WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runany where”哲學。 1)itusesbytiesebyTecodeThatrunsonAnyJvm-備用Platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中編寫平台特定代碼的原因包括訪問特定操作系統功能、與特定硬件交互和優化性能。 1)使用JNA或JNI訪問Windows註冊表;2)通過JNI與Linux特定硬件驅動程序交互;3)通過JNI使用Metal優化macOS上的遊戲性能。儘管如此,編寫平台特定代碼會影響代碼的可移植性、增加複雜性、可能帶來性能開銷和安全風險。

Java將通過雲原生應用、多平台部署和跨語言互操作進一步提昇平台獨立性。 1)雲原生應用將使用GraalVM和Quarkus提升啟動速度。 2)Java將擴展到嵌入式設備、移動設備和量子計算機。 3)通過GraalVM,Java將與Python、JavaScript等語言無縫集成,增強跨語言互操作性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器